你试一下这个。
应该都达到你说的了。
-------------------------------------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class AppStudent extends JFrame {
private JTextField nameField;
private JTextField sexField;
private JTextField ageField;
private JTextField stunoField;
private JTextField clazzField;
private JTextField facultyField;
private JTable table;
private DefaultTableModel model;
private String[] columns = { "姓名", "性别", "年龄", "学号", "班级", "系别" };
private List data;
private Student tmp;
// 姓名,性别,年龄,学号,班级,系别
public AppStudent() {
data = new ArrayList();
getContentPane().setLayout(null);
JLabel lblName = new JLabel("姓名");
lblName.setBounds(12, 10, 50, 13);
getContentPane().add(lblName);
nameField = new JTextField();
nameField.setBounds(74, 7, 96, 19);
getContentPane().add(nameField);
nameField.setColumns(10);
JLabel lblSex = new JLabel("性别");
lblSex.setBounds(182, 10, 50, 13);
getContentPane().add(lblSex);
sexField = new JTextField();
sexField.setBounds(244, 7, 96, 19);
getContentPane().add(sexField);
sexField.setColumns(10);
JLabel lblAge = new JLabel("年龄");
lblAge.setBounds(352, 10, 50, 13);
getContentPane().add(lblAge);
ageField = new JTextField();
ageField.setBounds(414, 7, 96, 19);
getContentPane().add(ageField);
ageField.setColumns(10);
JLabel lblStuno = new JLabel("学号");
lblStuno.setBounds(12, 36, 50, 13);
getContentPane().add(lblStuno);
stunoField = new JTextField();
stunoField.setColumns(10);
stunoField.setBounds(74, 33, 96, 19);
getContentPane().add(stunoField);
JLabel lblClass = new JLabel("班级");
lblClass.setBounds(182, 36, 50, 13);
getContentPane().add(lblClass);
clazzField = new JTextField();
clazzField.setColumns(10);
clazzField.setBounds(244, 33, 96, 19);
getContentPane().add(clazzField);
JLabel lblFaculty = new JLabel("系别");
lblFaculty.setBounds(352, 36, 50, 13);
getContentPane().add(lblFaculty);
facultyField = new JTextField();
facultyField.setColumns(10);
facultyField.setBounds(414, 33, 96, 19);
getContentPane().add(facultyField);
JButton btnAdd = new JButton("add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
add();
}
});
btnAdd.setBounds(192, 59, 67, 21);
getContentPane().add(btnAdd);
JButton btnDel = new JButton("del");
btnDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
del();
}
});
btnDel.setBounds(271, 59, 58, 21);
getContentPane().add(btnDel);
JButton btnUpdate = new JButton("update");
btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
}
});
btnUpdate.setBounds(352, 59, 77, 21);
getContentPane().add(btnUpdate);
JButton btnFind = new JButton("find");
btnFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
find();
}
});
btnFind.setBounds(441, 59, 69, 21);
getContentPane().add(btnFind);
model = new DefaultTableModel(columns, 0);
table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
nameField.setText((String) table.getValueAt(row, 0));
sexField.setText((String) table.getValueAt(row, 1));
ageField.setText((String) table.getValueAt(row, 2));
stunoField.setText((String) table.getValueAt(row, 3));
clazzField.setText((String) table.getValueAt(row, 4));
facultyField.setText((String) table.getValueAt(row, 5));
tmp = getInput();
}
});
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(12, 90, 571, 248);
getContentPane().add(scrollPane);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(601, 380);
setResizable(false);
setVisible(true);
}
private Student getInput() {
Student stu = new Student();
stu.name = nameField.getText();
stu.sex = sexField.getText();
stu.age = ageField.getText();
stu.stuno = stunoField.getText();
stu.clazz = clazzField.getText();
stu.faculty = facultyField.getText();
return stu;
}
private void add() {
data.add(getInput());
showTable();
}
private void del() {
for (int i = 0; i < data.size(); i++) {
if (tmp.equals(data.get(i))) {
data.remove(i);
break;
}
}
showTable();
}
private void update() {
Student stu = getInput();
for (int i = 0; i < data.size(); i++) {
if (tmp.equals(data.get(i))) {
data.remove(i);
data.add(i, stu);
break;
}
}
showTable();
}
private void find() {
removeRows();
Student stu = getInput();
for (int i = 0; i < data.size(); i++) {
Student tmp = (Student) data.get(i);
if (tmp.equals(stu)) {
model.addRow(tmp.toArray());
break;
}
}
}
private void showTable() {
removeRows();
for (int i = 0; i < data.size(); i++) {
Student stu = (Student) data.get(i);
model.addRow(stu.toArray());
}
}
private void removeRows() {
while (model.getRowCount() > 0) {
model.removeRow(0);
}
}
public static void main(String[] args) {
new AppStudent();
}
}
class Student implements Serializable {
public String name;
public String sex;
public String age;
public String stuno;
public String clazz;
public String faculty;
public boolean equals(Object obj) {
return equals((Student) obj);
}
public boolean equals(Student obj) {
boolean isName = true;
if (obj.name != null && !"".equals(obj.name)) {
isName = name.equals(obj.name);
}
boolean isSex = true;
if (obj.sex != null && !"".equals(obj.sex)) {
isSex = sex.equals(obj.sex);
}
boolean isAge = true;
if (obj.age != null && !"".equals(obj.age)) {
isAge = age.equals(obj.age);
}
boolean isStuno = true;
if (obj.stuno != null && !"".equals(obj.stuno)) {
isStuno = stuno.equals(obj.stuno);
}
boolean isClazz = true;
if (obj.clazz != null && !"".equals(obj.clazz)) {
isClazz = clazz.equals(obj.clazz);
}
boolean isfaculty = true;
if (obj.faculty != null && !"".equals(obj.faculty)) {
isfaculty = faculty.equals(obj.faculty);
}
return isName && isSex && isAge && isStuno && isClazz && isfaculty;
}
public String[] toArray() {
return new String[] { name, sex, age, stuno, clazz, faculty };
}
}
温馨提示:内容为网友见解,仅供参考