📄 communication.java
字号:
/*
* 创建日期 2006-3-13
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package addresslist;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
* @author CHUPING
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class Communication extends JFrame{
JLabel title = new JLabel("communication");
JLabel name = new JLabel("name");
JLabel zip =new JLabel("zip");
JLabel address = new JLabel("address");
JLabel telephone = new JLabel("telephone");
JLabel mobile =new JLabel("mobile");
JLabel email = new JLabel("email");
JLabel qqNum = new JLabel("qqNum");
JTextField nametext = new JTextField();
JTextField ziptext = new JTextField();
JTextField addtext = new JTextField();
JTextField teltext = new JTextField();
JTextField mobtext = new JTextField();
JTextField emailtext = new JTextField();
JTextField qqNumtext = new JTextField();
Font font = new Font("TimersRoman",Font.ITALIC,40);
JButton add = new JButton("add");
JButton find = new JButton("find");
JButton clear = new JButton("clear");
JButton exit = new JButton("exit");
ArrayList al =new ArrayList ();
Communication(String s){
super(s);
Container cp =getContentPane();
cp.setLayout(null);
title.setFont(font);
title.setBounds(130,10,300,60);
name.setBounds(50,70,75,25);
zip.setBounds(50,120,75,25);
address.setBounds(50,170,75,25);
telephone.setBounds(50,220,75,25);
mobile.setBounds(50,270,75,25);
email.setBounds(50,320,75,25);
qqNum.setBounds(50,370,75,25);
nametext.setBounds(150,70,100,25);
ziptext.setBounds(150,120,100,25);
addtext.setBounds(150,170,250,25);
teltext.setBounds(150,220,150,25);
mobtext.setBounds(150,270,150,25);
emailtext.setBounds(150,320,250,25);
qqNumtext.setBounds(150,370,150,25);
add.setBounds(50,400,75,25);
find.setBounds(150,400,75,25);
clear.setBounds(250,400,75,25);
exit.setBounds(350,400,75,25);
add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(nametext.getText().equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null,"无法添加名字为空的记录","Message",JOptionPane.INFORMATION_MESSAGE);
nametext.setText("");
ziptext.setText("");
addtext.setText("");
teltext.setText("");
mobtext.setText("");
emailtext.setText("");
qqNumtext.setText("");
return;
}
Note note = new Note();
note.name = nametext.getText();
note.zip=ziptext.getText();
note.address = addtext.getText();
note.telephone = teltext.getText();
note.mobile = mobtext.getText();
note.email = emailtext.getText();
note.qqNum = qqNumtext.getText();
try{
ObjectInputStream in = new ObjectInputStream(new FileInputStream("note.txt"));
al = (ArrayList)in.readObject();
in.close();
}catch(Exception ex){}
try{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("note.txt",true));
Note temp = new Note();
int i;
for(i = 0;i < al.size();i++){
temp=(Note)al.get(i);
if(temp.name.equalsIgnoreCase(nametext.getText()))
break;
}
if(al.size()!=0&&i!=al.size()){
JOptionPane.showMessageDialog(null,"已经存在此记录","Message",JOptionPane.INFORMATION_MESSAGE);
}
else{
al.add(note);
out.writeObject(al);
}
out.close();
}catch (Exception ex){}
nametext.setText("");
ziptext.setText("");
addtext.setText("");
teltext.setText("");
mobtext.setText("");
emailtext.setText("");
qqNumtext.setText("");
}
});
find.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("note.txt"));
al = (ArrayList)in.readObject();
in.close();
}catch(Exception ex){}
Note temp = new Note();
int i;
for (i = 0;i<al.size();i++){
temp = (Note)al.get(i);
if(temp.name.equalsIgnoreCase(nametext.getText()))
break;
}
if(al.size()!=0&&i!=al.size()){
ziptext.setText(temp.zip);
addtext.setText(temp.address);
teltext.setText(temp.telephone);
mobtext.setText(temp.mobile);
emailtext.setText(temp.email);
qqNumtext.setText(temp.qqNum);
}
else{
nametext.setText("");
ziptext.setText("");
addtext.setText("");
teltext.setText("");
mobtext.setText("");
emailtext.setText("");
qqNumtext.setText("");
JOptionPane.showMessageDialog(null,"无此记录","Message",JOptionPane.INFORMATION_MESSAGE);
}
}
});
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.txt"));
al.clear();
out.close();
}catch(Exception ex){}
nametext.setText("");
ziptext.setText("");
addtext.setText("");
teltext.setText("");
mobtext.setText("");
emailtext.setText("");
qqNumtext.setText("");
}
});
exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(1);
}
});
cp.add(title);
cp.add(name);
cp.add(zip);
cp.add(address);
cp.add(telephone);
cp.add(mobile);
cp.add(email);
cp.add(qqNum);
cp.add(nametext);
cp.add(ziptext);
cp.add(addtext);
cp.add(teltext);
cp.add(mobtext);
cp.add(emailtext);
cp.add(qqNumtext);
cp.add(add);
cp.add(find);
cp.add(clear);
cp.add(exit);
}
public static void main(String[] args) {
Communication com = new Communication("通讯录");
com.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
com.setSize(500,500);
com.setVisible(true);
}
}
class Note implements Serializable{
public String name;
public String zip;
public String address;
public String telephone;
public String mobile;
public String email;
public String qqNum;
public Note(){}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -