package view;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import util.Student;
import util.XmlUtil;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class App extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
private JButton btnAdd;
private JButton btnDel;
private JButton btnEdit;
private JTextField textSearch;
private JButton btnSearch;
private static DefaultTableModel dtm;
public static List<Student> students;
private JButton btnRefresh;
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
new App();
}
public App() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 750, 394);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 59, 714, 287);
contentPane.add(scrollPane);
String[] orderHeader = { "学号", "姓名", "性别", "年龄", "出生日期", "手机号码", "电子邮箱", "家庭住址", "专业" };
dtm = new DefaultTableModel(null, orderHeader);
table = new JTable(dtm) {
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTableHeader headerOrder = table.getTableHeader();
headerOrder.setReorderingAllowed(false);
scrollPane.setViewportView(table);
btnDel = new JButton("\u5220\u9664");
btnDel.addActionListener(this);
btnDel.setBounds(412, 18, 93, 23);
contentPane.add(btnDel);
btnEdit = new JButton("\u7F16\u8F91");
btnEdit.addActionListener(this);
btnEdit.setBounds(515, 18, 93, 23);
contentPane.add(btnEdit);
btnAdd = new JButton("\u6DFB\u52A0");
btnAdd.addActionListener(this);
btnAdd.setBounds(309, 18, 93, 23);
contentPane.add(btnAdd);
JLabel lblSearch = new JLabel("\u540D\u5B57\uFF1A");
lblSearch.setHorizontalAlignment(SwingConstants.RIGHT);
lblSearch.setBounds(10, 21, 54, 15);
contentPane.add(lblSearch);
textSearch = new JTextField();
textSearch.setBounds(74, 18, 117, 21);
contentPane.add(textSearch);
textSearch.setColumns(10);
btnSearch = new JButton("\u67E5\u627E");
btnSearch.addActionListener(this);
btnSearch.setBounds(201, 17, 93, 23);
contentPane.add(btnSearch);
btnRefresh = new JButton("\u5237\u65B0");
btnRefresh.addActionListener(this);
btnRefresh.setBounds(618, 18, 93, 23);
contentPane.add(btnRefresh);
setTitle("学生通讯录");
load();
setVisible(true);
}
/**
* 载入数据
*/
public static void load() {
dtm.setRowCount(0);
students = XmlUtil.readXml("students.xml");
for (Student student : students) {
dtm.addRow(student.toTableRow());
}
}
/**
* 事件处理
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnSearch) {
String name = textSearch.getText();
dtm.setRowCount(0);
for (Student student : students) {
if (name.equals(student.getName())) {
dtm.addRow(student.toTableRow());
}
}
} else if (e.getSource() == btnAdd) {
new AddStudent();
} else if (e.getSource() == btnDel) {
int rowIndex = table.getSelectedRow();
if (rowIndex != -1) {
String no = (String) dtm.getValueAt(rowIndex, 0);
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getNo().equals(no)) {
students.remove(i);
XmlUtil.writeXml(students, "students.xml");
load();
}
}
} else {
JOptionPane.showMessageDialog(new JPanel(), "请选择删除的学生!", "提示", JOptionPane.WARNING_MESSAGE);
}
} else if (e.getSource() == btnEdit) {
int rowIndex = table.getSelectedRow();
if (rowIndex != -1) {
String no = (String) dtm.getValueAt(rowIndex, 0);
for (Student student : students) {
if (student.getNo().equals(no)) {
new EditStudent(student);
}
}
} else {
JOptionPane.showMessageDialog(new JPanel(), "请选择编辑的学生!", "提示", JOptionPane.WARNING_MESSAGE);
}
} else if (e.getSource() == btnRefresh) {
load();
}
}
}