xz_911005 2019-12-08 17:57 采纳率: 0%
浏览 175

代码能运行,但添加信息时显示错误,不知道哪错了,求大佬帮忙改正正确的?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class Note implements Serializable{
public String name;
public String address;
public String telephone;
public Note(){}
}
public class AddressListSystem extends JFrame{
JLabel title=new JLabel("我的通讯录管理");
JLabel name=new JLabel("姓名");
JLabel address=new JLabel("地址");
JLabel telephone=new JLabel("电话");
JTextField jtxtname=new JTextField();
JTextField jtxtaddr=new JTextField();
JTextField jtxttel=new JTextField();
Font font=new Font("TimersRoman",Font.BOLD,30);
JButton jbtadd=new JButton("添加");
JButton jbtfind=new JButton("查找");
JButton jbtclear=new JButton("清空");
JButton jbtexit=new JButton("退出");
ArrayList al=new ArrayList();
AddressListSystem(String s){
super(s);
Container cp=getContentPane();
cp.setBackground(Color.PINK);
cp.setLayout(null);
title.setFont(font);
title.setForeground(Color.BLUE);
title.setBounds(130,20,300,60);
name.setBounds(50,150,75,25);
address.setBounds(50,220,75,25);
telephone.setBounds(50,310,75,25);
jtxtname.setBounds(150,150,100,25);
jtxtaddr.setBounds(150,220,100,25);
jtxttel.setBounds(150,310,200,25);
jbtadd.setBounds(50,400,75,25);
jbtfind.setBounds(150,400,75,25);
jbtclear.setBounds(250,400,75,25);
jbtexit.setBounds(350,400,75,25);
jbtadd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jtxtname.getText().equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null,"无法添加名字为空的记录","错误提示",JOptionPane.INFORMATION_MESSAGE);
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
return;
}
Note note=new Note();
note.name=jtxtname.getText();
note.address=jtxtaddr.getText();
note.telephone=jtxttel.getText();
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
al=(ArrayList)in.readObject();
in.close();
}catch(Exception ex){
System.out.println( "添加信息时文件输入有误哦");
}
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(jtxtname.getText()))
break;
}
if(!al.isEmpty()&&i!=al.size()){
JOptionPane.showMessageDialog(null,"已经存在此记录","错误提示",JOptionPane.INFORMATION_MESSAGE);
}
else{
al.add(note);
out.writeObject(al);
}
out.close();
}catch(Exception ex){
System.out.println("添加信息时文件输出有误哦");
}
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
}
});
jbtfind.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){
System.out.println("查询时文件打开有误哦");
}
Note temp=new Note();
int i;
for(i=0;i<al.size();i++){
temp=(Note)al.get(i);
if(temp.name.equalsIgnoreCase(jtxtname.getText()))
break;
}
if(!al.isEmpty()&&i!=al.size()){
jtxtaddr.setText(temp.address);
jtxttel.setText(temp.telephone);
}
else{
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
JOptionPane.showMessageDialog(null,"无此记录哦","温馨提示",JOptionPane.INFORMATION_MESSAGE);
}
}
});
jbtclear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.dat"));
al.clear();
out.close();
}catch(Exception ex){
System.out.println("清除文件时有误哦");
}
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
}
});

    jbtexit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            try{
                System.exit(1);
            }catch(Exception ex){
                System.out.println("退出系统时出错啦");
            }
        }
    });
    cp.add(title);
    cp.add(name);
    cp.add(address);
    cp.add(telephone);
    cp.add(jtxtname);
    cp.add(jtxtaddr);
    cp.add(jtxttel);
    cp.add(jbtadd);
    cp.add(jbtfind);
    cp.add(jbtclear);
    cp.add(jbtexit);
}

public static void main(String[] args) {
AddressListSystem als=new AddressListSystem("我的通讯录管理系统");
als.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
als.setSize(500,550);
als.setVisible(true);
}
}

  • 写回答

1条回答 默认 最新

  • 关注

    图片说明
    帮你试了一下,问题是文件读写的时候有问题,你参考这个文章的办法改改试试
    https://blog.csdn.net/mingyang_2016/article/details/75208117

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算