【初学者】请帮我看看这道题。谢谢您!
我写了一个java通讯录(基于图形界面),现在要求把通讯录中的联系人信息保存在文本文件中,实现联系人在文本文件中的查找、修改、删除、增加等信息。(我的代码是正确的,我已经运行过了)代码如下:
import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
public class Management {
int width=680,height=450;
static JFrame frame;
JButton newStudent,insert,dele;
JList list;
JTextField tname,tgender,tnumber,tmobilePhone,temail;
JTextArea taddr;
static Map<String,Student>students=new HashMap<String,Student>();
public static void main(String []args){
new Management().launchFrame();
}
public void launchFrame(){
JLabel lname,lgender,lnumber,lmobilePhone,lemail,laddr;
frame=new JFrame("学生通讯录");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(this.width,this.height);
frame.setResizable(false);
frame.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-this.width)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-this.height)/2);
newStudent=new JButton("新建");
insert=new JButton("添加");
dele=new JButton("删除");
JPanel jp=new JPanel();
jp.add(newStudent);
jp.add(insert);
jp.add(dele);
jp.setBorder(new TitledBorder("选择操作:"));
frame.add(jp,BorderLayout.NORTH);
list=new JList();
list.setFixedCellWidth(200);
JScrollPane jsp=new JScrollPane(list);
jsp.setBorder(new TitledBorder("查询窗口(根据学号查询)"));
this.launchStudents();
frame.add(jsp,BorderLayout.WEST);
lname=new JLabel("姓名:");
lgender=new JLabel("性别:");
lnumber=new JLabel("学号:");
lmobilePhone=new JLabel("电话:");
lemail=new JLabel("邮箱:");
laddr=new JLabel("住址:");
tname=new JTextField(10);
tgender=new JTextField(6);
tnumber=new JTextField(12);
tmobilePhone=new JTextField(8);
temail=new JTextField(10);
taddr=new JTextArea(3,20);
taddr.setLineWrap(true);
JPanel contentPanel=new JPanel();
contentPanel.setBorder(new TitledBorder("信息卡:"));
FlowLayout fly=(FlowLayout)contentPanel.getLayout();
fly.setAlignment(FlowLayout.LEFT);
contentPanel.setLayout(fly);
JPanel content1=new JPanel();
GridBagLayout gb1=new GridBagLayout();
GridBagConstraints c=new GridBagConstraints();
c.fill=GridBagConstraints.BOTH;
c.insets=new Insets(5,5,5,5);
c.weightx=1.0;
gb1.addLayoutComponent(lname,c);
content1.add(lname);
gb1.addLayoutComponent(tname, c);
content1.add(tname);
gb1.addLayoutComponent(lgender, c);
content1.add(lgender);
//c.gridwidth=GridBagConstraints.REMAINDER;
gb1.addLayoutComponent(tgender, c);
content1.add(tgender);
c.gridwidth=GridBagConstraints.REMAINDER;
c.gridheight=8;
c.gridwidth=1;
c.gridheight=1;
c.gridx=0;
c.gridy=1;
gb1.addLayoutComponent(lnumber, c);
content1.add(lnumber);
c.gridx=1;
c.gridwidth=2;
gb1.addLayoutComponent(tnumber, c);
content1.add(tnumber);
c.gridwidth=1;
c.gridx=0;
c.gridy=2;
gb1.addLayoutComponent(lmobilePhone,c);
content1.add(lmobilePhone);
c.gridx=1;
gb1.addLayoutComponent(tmobilePhone, c);
content1.add(tmobilePhone);
c.gridx=0;
c.gridy=3;
c.gridwidth=1;
c.gridx=0;
gb1.addLayoutComponent(lemail, c);
content1.add(lemail);
c.gridx=1;
gb1.addLayoutComponent(temail,c);
content1.add(temail);
c.gridx=0;
c.gridy=5;
c.gridwidth=1;
gb1.addLayoutComponent(laddr, c);
content1.add(laddr);
c.gridx=1;
c.gridwidth=3;
c.gridheight=3;
gb1.addLayoutComponent(taddr, c);
content1.add(taddr);
content1.setLayout(gb1);
contentPanel.add(content1);
frame.add(contentPanel,BorderLayout.CENTER);
frame.setVisible(true);
MyListener listener=new MyListener();
newStudent.addActionListener(listener);
insert.addActionListener(listener);
dele.addActionListener(listener);
list.addListSelectionListener(listener);
}
public void launchStudents(){
List<String> nums=new ArrayList<String>();
Iterator<String> it=students.keySet().iterator();
while(it.hasNext()){
String s=it.next();
nums.add(s);
}
list.setListData(nums.toArray());
}
class MyListener implements ListSelectionListener,ActionListener{
public void valueChanged(ListSelectionEvent e){
String num=list.getSelectedValue().toString();
Student s=students.get(num);
setContent(s);
}
public void actionPerformed(ActionEvent e){
String type=e.getActionCommand();
if(type.equals("新建")){
setContent();
}else if(type.equals("添加")){
if(tname.getText().length()>0&&tnumber.getText().length()>0){
newStudent();
launchStudents();
}else{
JOptionPane.showMessageDialog(frame,"请输入完整的信息!");
}
}else if(type.equals("删除")){
String num=list.getSelectedValue().toString();
delStudent(num);
launchStudents();
}
}
public void newStudent(){
String number=tnumber.getText();
Student s=new Student(number,tname.getText(),tgender.getText(),tmobilePhone.getText(),temail.getText(),taddr.getText());
students.put(number,s);
}
public void delStudent(String num){
students.remove(num);
}
public void setContent(){
tnumber.setText("");
tname.setText("");
tgender.setText("");
tmobilePhone.setText("");
temail.setText("");
taddr.setText("");
}
public void setContent(Student s){
tnumber.setText(s.getNum());
tname.setText(s.getName());
tgender.setText(s.getGender());
tmobilePhone.setText(s.getMobilePhone());
temail.setText(s.getMail());
taddr.setText(s.getAdd());
}
}
}
import
import
import
public class Student {
private String num;
private String name;
private String gender;
private String mobilePhone;
private String email;
private String add;
public Student(){
super();
}
public Student(String num,String name,String gender,String mobilePhone, String email,String add){
super();
this.num=num;
this.name=name;
this.gender=gender;
this.mobilePhone=mobilePhone;
this.email=email;
this.add=add;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getMobilePhone(){
return mobilePhone;
}
public void setDorm(String mobilePhone){
this.mobilePhone=mobilePhone;
}
public String getMail(){
return email;
}
public void setMail(String email){
this.email=email;
}
public String getAdd(){
return add;
}
public void setAdd(String add){
this.add=add;
}
public String getNum(){
return num;
}
public String getGender(){
return gender;
}
public static BufferedReader bufread;
//指定文件路径和名称
private static String path = "E:/suncity.txt";
private static File filename = new File(path);
private static String readStr ="";
/**
* 创建文本文件.
* @throws IOException
*
*/
public static void creatTxtFile() throws IOException{
if (!filename.exists()) {
filename.createNewFile();
System.err.println(filename + "已创建!");
}
}
}