package a;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ModifyStudentView {
JTextField nameText;
JTextField sexText;
JTextField ageText;
List list;
JFrame frame;
int a;
File file = new File("C:/Users/cxjaq/Desktop/stu.txt");
StudentTableModel model;
public ModifyStudentView(List list, StudentTableModel model,int a) {
this.list = list;
this.model = model;
this.a=a;
}
public void init() {
frame = new JFrame();
frame.setSize(300, 400);
frame.setLocationRelativeTo(null);
frame.setTitle("修改学生");
JPanel mainPanel = (JPanel) frame.getContentPane();
BoxLayout boxLayout = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
mainPanel.setLayout(boxLayout);
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
mainPanel.add(panel1);
mainPanel.add(panel2);
mainPanel.add(panel3);
mainPanel.add(panel4);
// 姓名
JLabel nameLabel = new JLabel();
nameLabel.setText("姓名");
panel1.add(nameLabel);
nameText = new JTextField();
nameText.setPreferredSize(new Dimension(120, 30));
nameText.setText(list.get(a).getName());
panel1.add(nameText);
// 性别
JLabel sexLabel = new JLabel();
sexLabel.setText("性别");
panel2.add(sexLabel);
sexText = new JTextField();
sexText.setPreferredSize(new Dimension(120, 30));
sexText.setText(list.get(a).getSex());
panel2.add(sexText);
//年龄
JLabel ageLabel = new JLabel();
ageLabel.setText("年龄");
panel3.add(ageLabel);
ageText = new JTextField();
ageText.setPreferredSize(new Dimension(120, 30));
ageText.setText(String.valueOf(list.get(a).getAge()));
panel3.add(ageText);
JButton saveBtn = new JButton();
saveBtn.setText("保存");
saveBtn.setPreferredSize(new Dimension(90, 30));
panel4.add(saveBtn);
saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameText.getText();
String sex = sexText.getText();
int age = Integer.parseInt(ageText.getText());
list.get(a).setName(name);
list.get(a).setSex(sex);
list.get(a).setAge(age);
frame.dispose();
save();
model.fireTableDataChanged();
JOptionPane.showMessageDialog(null, "修改成功","标题",JOptionPane.WARNING_MESSAGE);
}
});
frame.setVisible(true);
}
public void save() {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
主类Student.java
List list = new ArrayList();
File file = new File("C:/Users/cxjaq/Desktop/stu.txt");
StudentTableModel model;
JTextField nameText;
JTextField sexText;
JTextField ageText;
int a=1;
JTable table ;
String name;
String sex;
String age;
JButton modifyBtn = new JButton();
modifyBtn.setText("修改");
modifyBtn.setPreferredSize(new Dimension(90, 30));
panel3.add(modifyBtn);
modifyBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//a=table.getSelectedRow();
//ModifyStudentView msv = new ModifyStudentView(list, model,a);
//msv.init();
try {
a=table.getSelectedRow();
ModifyStudentView msv = new ModifyStudentView(list, model,a);
msv.init();
} catch (ArrayIndexOutOfBoundsException e2) {
// TODO: handle exception
JOptionPane.showMessageDialog(null, "请选中修改项","标题",JOptionPane.WARNING_MESSAGE);
}
}
});