发布网友 发布时间:2022-04-23 10:07
共1个回答
热心网友 时间:2023-05-02 23:10
大致写了一下,你可以看看:
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);
}
}