谢过各位TT
public class ManageStudentPanel extends JPanel{
JTable table;
DefaultTableModel model;
JScrollPane scrollPane;
JButton button_searchAll;
String[] columnNames = {"姓名","学号","专业","班级名","得分","评价"};
public ManageStudentPanel() {
setLayout(null);
initComponents();
setVisible(true);
}
private void initComponents() {
//使用列名创建表格模型
model = new DefaultTableModel(null,columnNames);
table = new JTable(model);
//查询面板
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder("查询测试记录"));
panel.setBounds(40,20,570,70);
panel.setLayout(new FlowLayout(FlowLayout.CENTER,15,5));
add(panel);
button_searchAll = new JButton("查询全部");
button_searchAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchAllStudent();
}
});
panel.add(button_searchAll);
//将表格放进滚动窗格,数据才能滚动展示
scrollPane = new JScrollPane(table);
scrollPane.setViewportView(table);
scrollPane.setBounds(45, 100, 570, 320);
add(scrollPane);
}
//获取全部测试记录
public void searchAllStudent(){
//先清空表格数据
int count = model.getRowCount();
for (int i = 0; i < count; i++) {
model.removeRow(0);
}
//逐行添加
for (Student student : Database.students) {
model.addRow(student.toArray());
}
}
}
这段代码实现了一个可以显示学生信息的 GUI 程序,使用的是 Java 的 Swing 库。
这是一个将数据库中的学生信息显示在窗口上 的程序。望采纳!!!!