import省略
问题是我点查询按钮时候,文本域上显示不了信息。
其他代码如下:
import省略
其他代码如下:
public class face extends JFrame implements ActionListener
{ private static final long serialVersionUID = 1L; Student stuinfo=new Student();
JPanel jp1,jp2,jp3;
JButton jb1,jb2,jb3,jb4;
JLabel jl1,jl2,jl3,jl4;
JTextArea ja1,ja2,ja3,ja4;
File file;
Container ct=getContentPane();
HashMap<String,Student>hminfo;
//构造方法
public face()
{
super();
initialize();
} //初始化
void initialize()
{
//设置windows外观样式
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex){ex.printStackTrace();}
this.setSize(425, 366);
//this.setResizable(false);
this.setTitle("学籍管理系统部分功能");
ct.setLayout(new BorderLayout(15,15));
jp1=new JPanel(new GridLayout(4,2,10,10));
jp2=new JPanel();
//jp2=new JPanel(new GridLayout(1,4,15,15));
jp3=new JPanel();
jl1=new JLabel(" 姓名:");
jl2=new JLabel(" 学号:");
jl3=new JLabel(" 性别:");
jl4=new JLabel(" 出生日期:");
ja1=new JTextArea();
ja2=new JTextArea();
ja3=new JTextArea();
ja4=new JTextArea();
jb1=new JButton("录入");
jb2=new JButton("查询");
jb3=new JButton("重置");
jb4=new JButton("退出");
jp1.add(jl1);
jp1.add(ja1);
jp1.add(jl2);
jp1.add(ja2);
jp1.add(jl3);
jp1.add(ja3);
jp1.add(jl4);
jp1.add(ja4)
;
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
ct.add(jp1,BorderLayout.CENTER);
ct.add(jp2,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
this.setVisible(true);
hminfo=new HashMap<String,Student>();
file =new File("信息.txt");
//写入文件
if(!file.exists())
{
try
{
//创建输出文件流对象 //创建要写入文件流对象的基本类型对象流
//将Student 对象info按照学号映射学生信息将写入哈希表
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectout =new ObjectOutputStream(out);
objectout.writeObject(hminfo);
objectout.close();
out.close();
}
catch(Exception e){};
}
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
//重置
void clear()
{
ja1.setText(null);
ja2.setText(null);
ja3.setText(null);
ja4.setText(null);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
String number="";
number=ja2.getText();
if(number.length()>0)
{
try
{
FileInputStream inone=new FileInputStream(file);
ObjectInputStream intwo=new ObjectInputStream(inone);
hminfo=(HashMap<String,Student>)intwo.readObject();
inone.close();
intwo.close();
}
catch(Exception ee){}
if(hminfo.containsKey(number))
{
String warning="该生信息已经存在!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
clear();
}
else
{
String m="信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
stuinfo.setName(ja1.getText());
stuinfo.setNumber(ja2.getText());
stuinfo.setSex(ja3.getText());
stuinfo.setBirth(ja4.getText());
try
{
//创建输出文件流对象
FileOutputStream outone=new FileOutputStream(file);
//创建要写入文件流对象的基本类型对象流
ObjectOutputStream outtwo=new ObjectOutputStream(outone);
//将Student 对象info按照学号映射学生信息将写入哈希表
hminfo.put(number, stuinfo);
outtwo.writeObject(hminfo);
outone.close();
outtwo.close();
}
catch(Exception ee){}
}
}
clear();
}
//如果哈希表里有该信息则不予录入
}
else if(e.getSource()==jb2)
{
String number="";
number=ja2.getText();
System.out.print("aaaaaa");
if(number.length()>0)
{
System.out.print("bbbbbbb");
try
{
FileInputStream inone=new FileInputStream(file);
ObjectInputStream intwo=new ObjectInputStream(inone);
HashMap temp1=(HashMap<String,Student>)intwo.readObject(); if(temp1.isEmpty())
{System.out.print("cccccc");}
inone.close();
intwo.close();
if(hminfo.containsKey(number))
{
Student temp=new Student();
temp=hminfo.get(number);
ja1.setText(temp.getName());
ja2.setText(temp.getNumber());
ja3.setText(temp.getSex());
ja4.setText(temp.getBirth());
}
}
catch(Exception ee){}
}
}
else if(e.getSource()==jb3)
{
clear(); }
else if(e.getSource()==jb4)
{
System.exit(0);
}
}
public static void main(String args[])
{
new face();
}
} 第二个是
class Student
{
String name,number,birth,sex;
void setNumber(String number)
{
this.number=number;
}
String getNumber()
{
return number;
}
void setName(String name)
{
this.name=name;
}
String getName()
{
return name;
}
void setBirth(String birth)
{
this.birth=birth;
}
String getBirth()
{
return birth;
}
void setSex(String sex)
{
this.sex=sex;
}
String getSex()
{
return sex;
}
}
问题是我点查询按钮时候,文本域上显示不了信息。
其他代码如下:
import省略
其他代码如下:
public class face extends JFrame implements ActionListener
{ private static final long serialVersionUID = 1L; Student stuinfo=new Student();
JPanel jp1,jp2,jp3;
JButton jb1,jb2,jb3,jb4;
JLabel jl1,jl2,jl3,jl4;
JTextArea ja1,ja2,ja3,ja4;
File file;
Container ct=getContentPane();
HashMap<String,Student>hminfo;
//构造方法
public face()
{
super();
initialize();
} //初始化
void initialize()
{
//设置windows外观样式
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception ex){ex.printStackTrace();}
this.setSize(425, 366);
//this.setResizable(false);
this.setTitle("学籍管理系统部分功能");
ct.setLayout(new BorderLayout(15,15));
jp1=new JPanel(new GridLayout(4,2,10,10));
jp2=new JPanel();
//jp2=new JPanel(new GridLayout(1,4,15,15));
jp3=new JPanel();
jl1=new JLabel(" 姓名:");
jl2=new JLabel(" 学号:");
jl3=new JLabel(" 性别:");
jl4=new JLabel(" 出生日期:");
ja1=new JTextArea();
ja2=new JTextArea();
ja3=new JTextArea();
ja4=new JTextArea();
jb1=new JButton("录入");
jb2=new JButton("查询");
jb3=new JButton("重置");
jb4=new JButton("退出");
jp1.add(jl1);
jp1.add(ja1);
jp1.add(jl2);
jp1.add(ja2);
jp1.add(jl3);
jp1.add(ja3);
jp1.add(jl4);
jp1.add(ja4)
;
jp2.add(jb1);
jp2.add(jb2);
jp2.add(jb3);
jp2.add(jb4);
ct.add(jp1,BorderLayout.CENTER);
ct.add(jp2,BorderLayout.SOUTH);
jb1.addActionListener(this);
jb2.addActionListener(this);
jb3.addActionListener(this);
jb4.addActionListener(this);
this.setVisible(true);
hminfo=new HashMap<String,Student>();
file =new File("信息.txt");
//写入文件
if(!file.exists())
{
try
{
//创建输出文件流对象 //创建要写入文件流对象的基本类型对象流
//将Student 对象info按照学号映射学生信息将写入哈希表
FileOutputStream out=new FileOutputStream(file);
ObjectOutputStream objectout =new ObjectOutputStream(out);
objectout.writeObject(hminfo);
objectout.close();
out.close();
}
catch(Exception e){};
}
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
//重置
void clear()
{
ja1.setText(null);
ja2.setText(null);
ja3.setText(null);
ja4.setText(null);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
String number="";
number=ja2.getText();
if(number.length()>0)
{
try
{
FileInputStream inone=new FileInputStream(file);
ObjectInputStream intwo=new ObjectInputStream(inone);
hminfo=(HashMap<String,Student>)intwo.readObject();
inone.close();
intwo.close();
}
catch(Exception ee){}
if(hminfo.containsKey(number))
{
String warning="该生信息已经存在!";
JOptionPane.showMessageDialog(this,warning,"警告",JOptionPane.WARNING_MESSAGE);
clear();
}
else
{
String m="信息将被录入!";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
stuinfo.setName(ja1.getText());
stuinfo.setNumber(ja2.getText());
stuinfo.setSex(ja3.getText());
stuinfo.setBirth(ja4.getText());
try
{
//创建输出文件流对象
FileOutputStream outone=new FileOutputStream(file);
//创建要写入文件流对象的基本类型对象流
ObjectOutputStream outtwo=new ObjectOutputStream(outone);
//将Student 对象info按照学号映射学生信息将写入哈希表
hminfo.put(number, stuinfo);
outtwo.writeObject(hminfo);
outone.close();
outtwo.close();
}
catch(Exception ee){}
}
}
clear();
}
//如果哈希表里有该信息则不予录入
}
else if(e.getSource()==jb2)
{
String number="";
number=ja2.getText();
System.out.print("aaaaaa");
if(number.length()>0)
{
System.out.print("bbbbbbb");
try
{
FileInputStream inone=new FileInputStream(file);
ObjectInputStream intwo=new ObjectInputStream(inone);
HashMap temp1=(HashMap<String,Student>)intwo.readObject(); if(temp1.isEmpty())
{System.out.print("cccccc");}
inone.close();
intwo.close();
if(hminfo.containsKey(number))
{
Student temp=new Student();
temp=hminfo.get(number);
ja1.setText(temp.getName());
ja2.setText(temp.getNumber());
ja3.setText(temp.getSex());
ja4.setText(temp.getBirth());
}
}
catch(Exception ee){}
}
}
else if(e.getSource()==jb3)
{
clear(); }
else if(e.getSource()==jb4)
{
System.exit(0);
}
}
public static void main(String args[])
{
new face();
}
} 第二个是
class Student
{
String name,number,birth,sex;
void setNumber(String number)
{
this.number=number;
}
String getNumber()
{
return number;
}
void setName(String name)
{
this.name=name;
}
String getName()
{
return name;
}
void setBirth(String birth)
{
this.birth=birth;
}
String getBirth()
{
return birth;
}
void setSex(String sex)
{
this.sex=sex;
}
String getSex()
{
return sex;
}
}
try
{
FileInputStream inone=new FileInputStream(file);
ObjectInputStream intwo=new ObjectInputStream(inone);
hminfo=(HashMap <String,Student>)intwo.readObject();inone.close();
intwo.close();
if(hminfo.containsKey(number))
{
Student temp=new Student();
temp=hminfo.get(number);
ja1.setText(temp.getName());
ja2.setText(temp.getNumber());
ja3.setText(temp.getSex());
ja4.setText(temp.getBirth());
}
}
catch(Exception ee){}
}
hminfo=(HashMap <String,Student>)intwo.readObject();这个句有问题,hminfo.containsKey(number)不知道问什么不为真。
异常,Student必须继承Serializable接口
class Student implements Serializable