2021-01-01

``## java通讯录用户界面,信息保存到文件,包括添加删除查找等功能
package addressbook;import java.io.;import java.util.ArrayList;import java.util.;import javax.swing.;import java.awt.;import java.awt.event.*;class jaddress extends JFrame implements ActionListener{ //刚开始的界面 JFrame ad=new JFrame(“通讯录”); Container con=this.getContentPane(); JLabel peson=new JLabel(“联系人:”); JButton find=new JButton(“查找”); JButton inset=new JButton(“添加”); JButton delete=new JButton(“删除”); JButton jexit =new JButton(“退出”); JTextField jfind=new JTextField(); JButton findall=new JButton(“显示全部”); JTextArea c1=new JTextArea(); public jaddress(){ c1.setBounds(new Rectangle(50,120,600,400)); ad.add(c1); c1.setLineWrap(true); findall.setBounds(new Rectangle(20,70,100,30)); findall.addActionListener(this); ad.add(findall); peson.setBounds(new Rectangle(20,5,70,70)); jfind.setBounds(new Rectangle(470,5,190,50)); find.setBounds(new Rectangle(400,5,60,50)); ad.setLayout(null); ad.add(peson); ad.add(find); find.addActionListener(this); ad.add(jfind); inset.setBounds(new Rectangle(200,570,60,60)); delete.setBounds(new Rectangle(300,570,60,60)); jexit.setBounds(new Rectangle(400,570,60,60)); ad.add(inset); inset.addActionListener(this); ad.add(delete); delete.addActionListener(this); ad.add(jexit); jexit.addActionListener(this); ad.setSize(700,700); ad.setVisible(true); } public void actionPerformed(ActionEvent e) { //用来处理开始界面按钮功能 String actionCommand =e.getActionCommand(); if(actionCommand==“查找”) { if(jfind.getText().length()!=0) { try { int i=0; String str; char s; FileReader read=new FileReader (“t1.txt”); BufferedReader in=new BufferedReader(read); while((str=in.readLine())!=null) { if(str.equals(jfind.getText())true) { i=1; String s1=""; for(int j=0;j<5;j++) { s1=s1+str+" "; str=in.readLine(); } JFrame addnews=new JFrame(“查找结果”); JTextArea addnew=new JTextArea(10,20); addnew.setText(s1); addnews.add(addnew); addnews.setSize(400,200); addnews.setVisible(true); break; } } if(i0) { JFrame as=new JFrame(“查找结果”); JTextArea addnew=new JTextArea(10,20); addnew.setText(“找不到该联系人”); as.add(addnew); as.setSize(400,200); as.setVisible(true); } } catch(IOException e1) {} } else { tishi q1=new tishi(“请输入查找内容”); q1.gettishi(); } } else if(actionCommand==“添加”) { tianjia1 ti=new tianjia1(); } else if(actionCommand==“删除”) { deletedata da=new deletedata(); } else if(actionCommand==“显示全部”) { try { c1.setText(""); //先将文本框清空,否则后面会重复显示 c1.append(“姓名 电话 地址 邮箱 公司或学校”+"\n"); String str; int i=0; FileReader read=new FileReader (“t1.txt”); BufferedReader in=new BufferedReader(read); while((str=in.readLine())!=null) { c1.append(str+" “); i++; if(i%50) { c1.append("\n"); } } read.close(); }catch(IOException e1) {} } else if(actionCommand"退出”) { System.exit(1); } }}class deletedata extends JFrame implements ActionListener { JFrame frame=new JFrame(“删除”); JLabel jLable =new JLabel(“输入要删除的姓名:”); JTextField jf=new JTextField(); JButton jbutton1=new JButton(“确定”); JButton jbutton2=new JButton(“返回”); public deletedata(){ frame.setLayout(null); jLable.setBounds(new Rectangle(10,50,150,50)); jf.setBounds(new Rectangle(160,50,180,50)); jbutton1.setBounds(new Rectangle(50,200,70,50)); jbutton2.setBounds(new Rectangle(150,200,70,50)); frame.add(jLable); frame.add(jf); frame.add(jbutton1); jbutton1.addActionListener(this); frame.add(jbutton2); jbutton2.addActionListener(this); frame.setSize(400,400); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { String actionCommand=e.getActionCommand(); if(actionCommand==“确定”) { if(jf.getText().length()!=0) { char a[][]=new char[100][20]; String str,str1=""; int i=0,flag=0; try { FileReader read=new FileReader (“t1.txt”); BufferedReader in=new BufferedReader(read); while((str=in.readLine())!=null) { if(str.equals(jf.getText())true) { flag=1; for(i=0;i<4;i++) { str=in.readLine(); } } else str1=str1+str+"\r\n"; } if(flag0) { tishi q1=new tishi(“没有该联系人”); q1.gettishi(); } else { try { FileWriter f=new FileWriter(“t1.txt”); f.write(str1); f.close(); }catch(Exception e1) {} tishi q1=new tishi(“删除成功”); q1.gettishi(); } // System.out.print(str1); }catch(IOException e1) {} } else { tishi q=new tishi(“请输入姓名”); q.gettishi(); } } if(actionCommand==“返回”) { frame.setVisible(false); //关闭当前窗口 } }}class tishi{ String s; tishi(String s1) { s=s1; } void gettishi() { JFrame i=new JFrame(“提示”); JLabel p=new JLabel(s); i.add§; i.setLayout(null); p.setBounds(new Rectangle(50,50,100,50)); i.setSize(300,200); i.setVisible(true); }}class tianjia1 extends JFrame implements ActionListener //添加界面{ JFrame ins=new JFrame(“添加”); JLabel name=new JLabel(“姓名:”); JLabel phoneNumber=new JLabel(“电话:”); JLabel address=new JLabel(“地址:”); JLabel email=new JLabel(“邮箱:”); JLabel companyOrSchool=new JLabel(“公司或学校:”); JTextField inputname=new JTextField(); JTextField inputphoneNumber=new JTextField(); JTextField inputaddress=new JTextField(); JTextField inputemail=new JTextField(); JTextField inputcompanyOrSchool=new JTextField(); JButton retu=new JButton(“返回”); JLabel peson1=new JLabel(“新添加联系人:”); JButton save=new JButton(“保存”); public tianjia1(){ ins.setLayout(null); name.setBounds(new Rectangle(80,50,60,60)); inputname.setBounds(new Rectangle(150,50,200,50)); phoneNumber.setBounds(new Rectangle(80,130,60,60)); inputphoneNumber.setBounds(new Rectangle(150,130,200,50)); address.setBounds(new Rectangle(80,210,60,60)); email.setBounds(new Rectangle(80,290,60,60)); inputaddress.setBounds(new Rectangle(150,210,200,50)); inputemail.setBounds(new Rectangle(150,290,200,50)); companyOrSchool.setBounds(new Rectangle(80,370,80,60)); inputcompanyOrSchool.setBounds(new Rectangle(150,370,200,50)); retu.setBounds(new Rectangle(270,450,80,60)); ins.add(name); ins.add(inputname); ins.add(phoneNumber); ins.add(inputphoneNumber); ins.add(address); ins.add(inputaddress); ins.add(email); ins.add(inputemail); ins.add(companyOrSchool); ins.add(inputcompanyOrSchool); peson1.setBounds(new Rectangle(10,5,100,40)); ins.add(peson1); save.setBounds(new Rectangle(150,450,80,60)); ins.add(save); save.addActionListener(this); ins.add(retu); retu.addActionListener(this); ins.setSize(600,600); ins.setVisible(true); } public void actionPerformed(ActionEvent e) //添加界面的事件处理 { String actionCommand1=e.getActionCommand(); if(actionCommand1==“保存”) { boolean p=true; if§ { try { FileWriter fw=new FileWriter(“t1.txt”,true); PrintWriter pr=new PrintWriter (fw); //追加文件 if(inputname.getText().length()==0||inputphoneNumber.getText().length()==0||inputaddress.getText().length()==0||inputemail.getText().length()0||inputcompanyOrSchool.getText().length()0) { tishi q=new tishi(“信息不能为空”); q.gettishi(); p=false; } else { pr.println(inputname.getText()); pr.println(inputphoneNumber.getText()); pr.println(inputaddress.getText()); pr.println(inputemail.getText()); pr.println(inputcompanyOrSchool.getText()); fw.close(); } if(ptrue) { tishi q=new tishi(“保存成功”); q.gettishi(); } } catch(IOException e1) {} } } else if(actionCommand1"返回") { ins.setVisible(false); //关闭当前窗口 } }}public class addressbook { public static void main(String[] args) { jaddress ad=new jaddress(); }}

你可能感兴趣的:(笔记)