夙君戈 夙君戈
给我再去相信的勇气,越过谎言去拥抱你。
关注数: 190 粉丝数: 102 发帖数: 4,722 关注贴吧数: 45
求把程序改成文件保存! =。=,女生学编程伤不起,求大神改哈小程序。 求把下面的程序改成用文件保存,我应该是用数组保存的,希望在开头加个读取文件,然后按退出的时候可以写入文件!! import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JOptionPane; public class AddressBook extends Frame implements ActionListener{//1 private MenuItem noteAdd=new MenuItem("添加"); private MenuItem noteDel=new MenuItem("删除"); private MenuItem noteEdit=new MenuItem("修改"); private MenuItem noteSearch =new MenuItem("查找"); private MenuItem noteExit=new MenuItem("退出"); private List noteList=new List(); private String[][] message=new String[1000][7]; public Info notes=new Info(); public AddressBook(){//2 super("班级通讯录"); Menu note=new Menu("菜单"); note.add(noteAdd); note.add(noteDel); note.add(noteEdit); note.add(noteSearch); note.addSeparator(); note.add(noteExit); MenuBar bar=new MenuBar(); bar.add(note); setMenuBar(bar); add(noteList); noteAdd.addActionListener(this); noteDel.addActionListener(this); noteEdit.addActionListener(this); noteExit.addActionListener(this); noteSearch.addActionListener(this); noteList.addActionListener(this); addWindowListener(new WindowCloser()); setSize(400,200);setVisible(true); }//2 public void actionPerformed(ActionEvent e){//3 if (e.getSource()==noteAdd) {//4 notes.sex=null; notes.num=null; notes.qq=null; notes.mail=null; notes.name=null; notes.address=null; notes.telephone=null; int index=noteList.getItemCount(); EditDialog dlg=new EditDialog(this,"班级通讯录",notes,false); if (dlg.isOk){//5 message[index][0]=dlg.textNum.getText(); message[index][1]=dlg.textName.getText(); message[index][2]=dlg.textSex.getText(); message[index][3]=dlg.textTelephone.getText(); message[index][4]=dlg.textAddress.getText(); message[index][5]=dlg.textEmail.getText(); message[index][6]=dlg.textqq.getText(); if (message[index][0]!=null){//6String s="学号:"+message[index][0]+" 姓名:"+message[index][1]+" 性别:"+message[index][2]+" 电话:"+message[index][3]+" 地址:"+message[index][4]+" 邮箱:"+message[index][5]+" QQ:"+message[index][6];noteList.add(s); }//6}//5dlg.dispose();}//4 else if(e.getSource()==noteEdit||e.getSource()==noteList) {//7int i=noteList.getSelectedIndex(); if (i!=-1){//8notes.num=message[i][0]; notes.name=message[i][1]; notes.sex=message[i][2]; notes.telephone=message[i][3]; notes.address=message[i][4]; notes.mail=message[i][5]; notes.qq=message[i][6]; EditDialog dlg=new EditDialog(this,"修改通讯录",notes,false); if(dlg.isOk){//9message[i][0]=dlg.textNum.getText(); message[i][1]=dlg.textName.getText(); message[i][2]=dlg.textSex.getText(); message[i][3]=dlg.textTelephone.getText(); message[i][4]=dlg.textAddress.getText(); message[i][5]=dlg.textEmail.getText(); message[i][6]=dlg.textqq.getText(); String s="学号:"+message[i][0]+" 姓名:"+message[i][1]+" 性别:"+message[i][2]+" 电话:"+message[i][3]+" 地址:"+message[i][4]+" 邮箱:"+message[i][5]+" QQ:"+message[i][6];noteList.replaceItem(s,i);}//9dlg.dispose();}//8 }//7else if (e.getSource()==noteDel) {//10ConfirmDialog cd=new ConfirmDialog(this,"确认删除?","确认要删除这条通讯录信息吗?"); if(cd.close) {//11int index=noteList.getSelectedIndex(); for(int i=index;i<noteList.getItemCount()-1;i++) {//12for(int j=0;j<7;j++) message[i][j]=message[i+1][j];}//12 noteList.remove(index);}//11}//10 else if (e.getSource()==noteSearch){//13 EditDialog dlg=new EditDialog(this,"查找通讯录",notes,true); if(dlg.isOk){//14for(int i=0;i<noteList.getItemCount();i++) {//15String thisname=noteList.getItem(i).toString().split(" ")[0].split(":")[1];String thisnum=noteList.getItem(i).toString().split(" ")[30].split(":")[1]; if((dlg.name.equals(thisname) && dlg.num.equals(thisnum))||(dlg.name.equals(thisname) && (dlg.num.equals("")))||((dlg.name.equals("")) && dlg.num.equals(thisnum))) {//16 Object show = "查找成功!信息为:"+noteList.getItem(i).toString(); JOptionPane.showMessageDialog(null,show);return;}//16 }//15JOptionPane.showMessageDialog(null,"无此人信息!"); }//14 }//13 else if (e.getSource()==noteExit){//17 System.exit(0);}//17}//3 private class WindowCloser extends WindowAdapter{//18 public void windowClosing(WindowEvent we){//19 System.exit(0); }//19 }//18 public static void main(String [] args){//21 new AddressBook(); }//21 }//1 class EditDialog extends Dialog implements ActionListener{//22 private Button ok=new Button("确定"); private Button cancel=new Button("取消"); private Label labelNum=new Label(" 学号",Label.LEFT); private Label labelName=new Label(" 姓名",Label.LEFT); private Label labelSex=new Label(" 性别",Label.LEFT); private Label labelTelephone=new Label(" 电话",Label.LEFT); private Label labelAddress=new Label(" 地址",Label.LEFT); private Label labelEmail=new Label("E-mail",Label.LEFT); private Label labelqq=new Label(" qq",Label.LEFT); public String name=null;public String num=null; TextField textNum=new TextField(20); TextField textName=new TextField(20); TextField textSex=new TextField(20); TextField textTelephone=new TextField(20); TextField textAddress=new TextField(20); TextField textEmail=new TextField(20); TextField textqq=new TextField(20); public boolean isOk=false; private Info notes=new Info(); public EditDialog(Frame parent,String title,Info nt,boolean search){//23 super(parent,title,true); notes=nt; Panel pName=new Panel(); pName.setLayout(new FlowLayout()); pName.add(labelName); pName.add(textName); Panel pNum=new Panel(); pNum.setLayout(new FlowLayout()); pNum.add(labelNum); pNum.add(textNum); Panel pInfo=new Panel(); if(!search) {//24Panel pSex=new Panel(); pSex.setLayout(new FlowLayout()); pSex.add(labelSex); pSex.add(textSex); Panel pTelephone=new Panel(); pTelephone.setLayout(new FlowLayout()); pTelephone.add(labelTelephone); pTelephone.add(textTelephone); Panel pAddress=new Panel(); pAddress.setLayout(new FlowLayout()); pAddress.add(labelAddress); pAddress.add(textAddress); Panel pEmail=new Panel(); pEmail.setLayout(new FlowLayout()); pEmail.add(labelEmail); pEmail.add(textEmail); Panel pqq=new Panel(); pqq.setLayout(new FlowLayout()); pqq.add(labelqq); pqq.add(textqq); pInfo.setLayout(new GridLayout(7,1)); pInfo.add(pNum); pInfo.add(pName); pInfo.add(pSex); pInfo.add(pTelephone); pInfo.add(pAddress); pInfo.add(pEmail); pInfo.add(pqq); }//24else{//25 pInfo.setLayout(new GridLayout(2,1)); pInfo.add(pName); pInfo.add(pNum);}//25 Panel pButton=new Panel(); pButton.setLayout(new FlowLayout()); pButton.add(ok); pButton.add(cancel); add("Center",pInfo); add("South",pButton); textNum.setText(notes.num); textName.setText(notes.name); textSex.setText(notes.sex); textTelephone.setText(notes.telephone); textAddress.setText(notes.address); textEmail.setText(notes.mail); textqq.setText(notes.qq); ok.addActionListener(this); cancel.addActionListener(this); addWindowListener(new WindowCloser()); pack(); setVisible(true);}//23 public void actionPerformed(ActionEvent ae){//26 if (ae.getSource()==ok){//27 isOk=true; name=textName.getText(); num=textNum.getText(); setVisible(false); }//27 if (ae.getSource()==cancel){//28 isOk=false; setVisible(false); }//28 }//26 private class WindowCloser extends WindowAdapter{//29 public void windowClosing(WindowEvent ae){//30 isOk=false; EditDialog.this.setVisible(false); }//30 }//29 }//22 class ConfirmDialog extends Dialog implements ActionListener{//31 private Button okay=new Button("确定"); private Button cancel=new Button("取消"); private Label label=new Label(); public boolean close=false; public ConfirmDialog(Frame parent,String title,String question){//32 super(parent,title,true); label.setText(question); Panel buttons=new Panel(); buttons.setLayout(new FlowLayout()); buttons.add(okay); buttons.add(cancel); setLayout(new BorderLayout()); add("Center",label);add("South",buttons); okay.addActionListener(this); cancel.addActionListener(this);addWindowListener(new WindowCloser()); pack(); setVisible(true);}//32 private class WindowCloser extends WindowAdapter{//33 public void wndowClosing(WindowEvent we){//34 ConfirmDialog.this.close=false; ConfirmDialog.this.setVisible(false);}//34}//33 public void actionPerformed(ActionEvent ae){//35 close=(ae.getSource()==okay);setVisible(false);}//35 }//31 class Info{//36public String num; public String name; public String sex; public String telephone; public String address; public String mail; public String qq; }//36
1 下一页