📄 application.java
字号:
super(s);
name=new JLabel("姓名:",JLabel.RIGHT);
zip=new JLabel("邮政编码:",JLabel.RIGHT);
address=new JLabel("通信地址:",JLabel.RIGHT);
telephone=new JLabel("电话:",JLabel.RIGHT);
mobile=new JLabel("手机:",JLabel.RIGHT);
email=new JLabel("email:",JLabel.RIGHT);
nametext=new JTextField();
ziptext=new JTextField();
addtext=new JTextField();
teltext=new JTextField();
mobtext=new JTextField();
emailtext=new JTextField();
add=new JButton("添加");
find=new JButton("查找");
clear=new JButton("删除");
al=new ArrayList();
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
JPanel pane=new JPanel();
pane.setLayout(new GridLayout(6,2));
pane.add(name);
pane.add(nametext);
pane.add(zip);
pane.add(ziptext);
pane.add(address);
pane.add(addtext);
pane.add(telephone);
pane.add(teltext);
pane.add(mobile);
pane.add(mobtext);
pane.add(email);
pane.add(emailtext);
cp.add(pane,BorderLayout.CENTER);
JPanel buttonpane=new JPanel();
buttonpane.setLayout(new GridLayout(1,3));
buttonpane.add(add);
buttonpane.add(find);
buttonpane.add(clear);
cp.add(buttonpane,BorderLayout.SOUTH);
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("");
return;
}
Note note=new Note();
/**通过getText()方法从文本框中获取文本*/
note.name=nametext.getText();
note.zip=ziptext.getText();
note.address=addtext.getText();
note.telephone=teltext.getText();
note.mobile=mobtext.getText();
note.email=emailtext.getText();
/**对可能出现的异常进行捕获*/
try{/**ObjectInputStream和ObjectOutputStream用于对象串行化*/
ObjectInputStream in=new ObjectInputStream(
/**以note.dat来构建文件输入流对象*/
new FileInputStream("note.dat"));
al=(ArrayList)in.readObject();
in.close();
}catch(Exception ex){}
/**
*通过note.dat来创建一个文件输出流,再增添一个布尔型变量,因为
*为true,所以所写入的数据添加到文件后面,而不是将文件覆盖
*/
try{
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream("note.dat",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()){//ArrayList里已有记录
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("");
}
});
/**为find按钮添加ActionListener监听器,并且对可能出现的异常进行捕获*/
find.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectInputStream in=new ObjectInputStream(
new FileInputStream("note.dat"));
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);
}
/**
*如果要查找的姓名在记录中不存在,则各文本框清空,并且弹出提示对话框
*告诉你"无此记录"
*/
else{
nametext.setText("");
ziptext.setText("");
addtext.setText("");
teltext.setText("");
mobtext.setText("");
emailtext.setText("");
JOptionPane.showMessageDialog(null,"无此记录","Message",JOptionPane.INFORMATION_MESSAGE);}
}
});
/**为clear按钮添加ActionListener监听器,并且对可能出现的异常进行捕获*/
clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream("note.dat"));
al.clear();
out.close();
}catch(Exception ex){}
nametext.setText("");
ziptext.setText("");
addtext.setText("");
teltext.setText("");
mobtext.setText("");
emailtext.setText("");
}
});
setSize(300,200);//设置窗口尺寸
setResizable(true);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setVisible(true);
}
}
/**
*定义类Note,类继承了接口Serializable
*/
class Note implements Serializable{
public String name;
public String zip;
public String address;
public String telephone;
public String mobile;
public String email;
public Note(){}
}
class MyMedia{
String mediaName;
float price;
String press;
String artist;
MyMedia(String nametemp,float pricetemp,String presstemp,String artisttemp){
mediaName=nametemp;
price=pricetemp;
press=presstemp;
artist=artisttemp;
}
String getName(){
return mediaName;
}
float getPrice(){
return price;
}
String getPress(){
return press;
}
String getArtist(){
return artist;
}
void setName(String nametemp){
mediaName=nametemp;
}
void setPrice(float pricetemp){
price=pricetemp;
}
void setPress(String presstemp){
press=presstemp;
}
void setArtist(String artisttemp){
artist=artisttemp;
}
}
class MyCD extends MyMedia{
String cdISRC;
String cdPublisher;
MyCD(String cn,float cp,String cpr,String ca,String cpl,String ci){
super(cn,cp,cpr,ca);
cdISRC=ci;
cdPublisher=cpl;
}
String getCDISRC(){
return cdISRC;
}
String getCDPublisher(){
return cdPublisher;
}
void setCDISRC(String cdISRCtemp){
cdISRC=cdISRCtemp;
}
void setCDPublisher(String cdPublishertemp){
cdPublisher=cdPublishertemp;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -