java通讯录管理
1) 通讯录信息要包括姓名(FirstName,LastName)、通信地址、邮政编码、移动电话、电子邮件等,要求覆盖toString()方法以便将通讯录条目转换为字符串,并体现信息隐藏和封装的思想;
2) 要求能够按照姓名对其进行排序(提示:可实现Comparable接口,该接口中仅声明了一个方法:int compareTo(T o),若当前对象与比较的对象之间存在小于、等于和大于关系时,其返回值分别为-1、0和1,T为比较的对象类型);
3) 可通过姓名、电子邮件或移动电话进行查找,若找不到符合条件的记录,查找方法需抛出自定义异常,查找方法的返回类型为通讯录项的数组;
4) 设计通讯录管理程序的GUI界面,参考如下(包括菜单项,标题、表格、标签、编辑框和状态条)。该界面为不完整界面,其余部分应包括如“添加”、“修改”、“删除”、“查找”等功能按钮以及你认为需要的菜单项和按钮,并实现其对应的功能。表格中的当前项的详细信息应显示在表格下面的栏目中。所有功能设置均以合理为原则。
5) 通讯录的所有信息保存在一个文本文件中,其中每条记录占一行,每行中的不同字段通过逗号分割开来(如:姓名,通信地址,邮编,移动电话,电子邮件,因此若信息项中有逗号,需考虑转义处理)。软件启动时,应可自动从该文件中装载现有的通讯录信息。 展开
大致写了一下,你可以看看:
package noteBook;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
class guiView extends JFrame implements ActionListener {
JMenuBar jmb;
JMenu text;
JMenu edit;
JMenu help;
JMenuItem save;
JMenuItem change;
JMenuItem guide;
JLabel name;
JLabel address;
JLabel post;
JLabel mobilePhone;
JLabel email;
JTextField JT_name;
JTextField JT_address;
JTextField JT_post;
JTextField JT_mobilePhone;
JTextField JT_email;
JButton jOk;
JButton jCancel;
JTable table;
static DefaultTableModel dtm;
Vector<String> head;
int row = 0; // 用于标记当前行的位置
public guiView() {
super("通讯录管理程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 700);
setVisible(true);
Container con = this.getContentPane();
jmb = new JMenuBar();
this.setJMenuBar(jmb);
text = new JMenu("文件", true);
edit = new JMenu("编辑", true);
help = new JMenu("帮助", true);
save = new JMenuItem("保存");
change = new JMenuItem("修改");
guide = new JMenuItem("指导");
save.addActionListener(this);save.setActionCommand("save");
change.addActionListener(this);change.setActionCommand("change");
guide.addActionListener(this);guide.setActionCommand("guide");
text.add(save);edit.add(change);help.add(guide);
jmb.add(text);jmb.add(edit);jmb.add(help);
name = new JLabel("姓名:", Label.RIGHT);
address = new JLabel("通信地址:", Label.RIGHT);
post = new JLabel("邮编:", Label.RIGHT);
mobilePhone = new JLabel("移动电话:", Label.RIGHT);
email = new JLabel("电子邮件:", Label.RIGHT);
JT_name = new JTextField(12);
JT_address = new JTextField(28);
JT_post = new JTextField(12);
JT_mobilePhone = new JTextField(12);
JT_email = new JTextField(12);
jOk = new JButton("确定");
jCancel = new JButton("取消");
jOk.addActionListener(this);
jOk.setActionCommand("ok");
jCancel.addActionListener(this);
jCancel.setActionCommand("cancel");
JPanel panelSOUTH = new JPanel();
JPanel panelCENTER = new JPanel();
JPanel panelCENTERSouth = new JPanel();
JPanel panelCENTERCenter = new JPanel();
JPanel panelCENTERNorth = new JPanel();
JPanel panelNorth = new JPanel();
JPanel panelNorthSouth = new JPanel();
head = new Vector<String>();
head.addElement("姓名");
head.addElement("通信地址");
head.addElement("邮编");
head.addElement("移动电话");
head.addElement("电子邮件");
dtm = new DefaultTableModel(null, head);
showData();
table = new JTable(dtm);
table.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
row = table.getSelectedRow();
System.out.println(row);
getData(row);
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
JScrollPane jsp = new JScrollPane(table);
panelNorthSouth.setLayout(new GridLayout(1, 5));
panelNorthSouth.add(name);
panelNorthSouth.add(address);
panelNorthSouth.add(post);
panelNorthSouth.add(mobilePhone);
panelNorthSouth.add(email);
panelNorth.setLayout(new BorderLayout());
panelNorth.add(jsp,BorderLayout.CENTER);
panelNorth.add(panelNorthSouth, BorderLayout.SOUTH);
panelCENTERNorth.add(name);
panelCENTERNorth.add(JT_name);
panelCENTERNorth.add(email);
panelCENTERNorth.add(JT_email);
panelCENTERCenter.add(post);
panelCENTERCenter.add(JT_post);
panelCENTERCenter.add(mobilePhone);
panelCENTERCenter.add(JT_mobilePhone);
panelCENTERSouth.add(address);
panelCENTERSouth.add(JT_address);
panelCENTER.add(panelCENTERNorth, BorderLayout.NORTH);
panelCENTER.add(panelCENTERCenter, BorderLayout.CENTER);
panelCENTER.add(panelCENTERSouth, BorderLayout.SOUTH);
panelSOUTH.add(jOk);
panelSOUTH.add(jCancel);
con.add(panelNorth, BorderLayout.NORTH);
con.add(panelCENTER, BorderLayout.CENTER);
con.add(panelSOUTH, BorderLayout.SOUTH);
}
// 对点击首行,上一行,下一行,最后一行按钮触发的操作进行事件响应处理
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("ok")) {
String name = this.JT_name.getText();
String address = this.JT_address.getText();
String post = this.JT_post.getText();
String mobilePhone = this.JT_mobilePhone.getText();
String email = this.JT_email.getText();
saveData(name, address, post, mobilePhone, email);
showData();
table.setModel(dtm);
this.JT_name.setText("");
this.JT_address.setText("");
this.JT_post.setText("");
this.JT_mobilePhone.setText("");
this.JT_email.setText("");
}
else if (command.equals("cancel")) {
this.JT_name.setText("");
this.JT_address.setText("");
this.JT_post.setText("");
this.JT_mobilePhone.setText("");
this.JT_email.setText("");
}
else if(command.equals("save")){
// save all data in the table
}
else if(command.equals("change")){
// table allow to change some data
}
else if(command.equals("guide")){
// maybe show how to use it
}
else {
System.err.println("error!");
System.exit(-1);
}
}
private void showData() {
dtm = new DefaultTableModel(null, head);
File file = new File("F:/java/data.txt");
String str = null;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
str = br.readLine();
while (str != null) {
if (str == null) {
System.err.println("no data!");
break;
} else {
StringTokenizer st = new StringTokenizer(str, ",");// 每行信息的6个字段分别用空格隔开的
Vector<String> v = new Vector<String>();
v.addElement(st.nextToken());
v.addElement(st.nextToken());
v.addElement(st.nextToken());
v.addElement(st.nextToken());
v.addElement(st.nextToken());
dtm.addRow(v);
}
str = br.readLine();
}
br.close();// 一定要关闭资源
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveData(String name, String address, String post,
String mobilePhone, String email) {
File file = new File("F:/java/data.txt");
ArrayList list = null;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
list = new ArrayList();
String ss = "";
while ((ss = br.readLine()) != null)
list.add(ss);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
list.set(row, name + "," + address + "," + post + "," + mobilePhone
+ "," + email);
try {
PrintWriter pw = new PrintWriter(file);
for (int i = 0; i < list.size(); i++) {
pw.println(list.get(i));
}
list.clear();
list = null;
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// 通过flag标记当前读到了文件的第几行。然后循环读至该行
private void getData(int row) {
File file = new File("F:/java/data.txt");
String str = null;
StringTokenizer st;
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
for (int i = 0; i <= row; i++) {
str = br.readLine();
}
if (str == null) {
System.err.println("no data!");
} else {
st = new StringTokenizer(str, ",");// 每行信息的6个字段分别用空格隔开的
this.JT_name.setText(st.nextToken());
this.JT_address.setText(st.nextToken());
this.JT_post.setText(st.nextToken());
this.JT_mobilePhone.setText(st.nextToken());
this.JT_email.setText(st.nextToken());
}
br.close();// 一定要关闭资源
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] s) {
guiView c = new guiView();
c.setVisible(true);
}
}
广告 您可能关注的内容 |