| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 994 人关注过本帖
标题:【初学者】请帮我看看这道题。谢谢您!
只看楼主 加入收藏
邵16
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-8
结帖率:0
收藏
已结贴  问题点数:5 回复次数:8 
【初学者】请帮我看看这道题。谢谢您!
我写了一个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 + "已创建!");
  }
}



}





搜索更多相关主题的帖子: 文本文件 联系人 public 通讯录 import 
2014-05-08 11:33
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
收藏
得分:1 
想让别人看啥呢?
2014-05-10 09:53
浊世清。
Rank: 1
等 级:新手上路
帖 子:1
专家分:1
注 册:2014-5-10
收藏
得分:1 
你要别人看什么。都没说出来。
2014-05-10 17:36
星112
Rank: 5Rank: 5
来 自:山西朔州
等 级:职业侠客
威 望:2
帖 子:94
专家分:342
注 册:2012-6-22
收藏
得分:1 
然后想表达个啥呢??
2014-05-11 12:56
鱼和肉
Rank: 1
等 级:新手上路
帖 子:5
专家分:1
注 册:2014-5-11
收藏
得分:1 
都不写注释让别人怎么看。
2014-05-11 20:23
LX我爱编程
Rank: 1
等 级:新手上路
帖 子:11
专家分:1
注 册:2014-2-27
收藏
得分:1 
你这是初学者??
2014-05-14 16:47
砖家的谎言
Rank: 12Rank: 12Rank: 12
等 级:禁止访问
威 望:30
帖 子:693
专家分:3898
注 册:2013-12-6
收藏
得分:1 
?在网上抄的程序吧,你要问什么问题呢

我不是砖家,要努力成为砖家。
2014-05-15 13:25
xl881221
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:30
专家分:177
注 册:2014-3-23
收藏
得分:0 
楼主的意思是想实现,把添加的联系人的信息都保存到一个文件里,再打开程序时还能显示出来。
现在这个程序只是把联系人信息保存到static Map<String,Student>students=new HashMap<String,Student>()里,关闭程序后数据都没了
2014-05-16 13:54
梓明
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-3-4
收藏
得分:0 
这是初学者?
2015-03-04 18:37
快速回复:【初学者】请帮我看看这道题。谢谢您!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038561 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved