Javaswing学生信息管理系统
时间: 2023-06-22 16:20:57 浏览: 140
以下是一个简单的Java Swing学生信息管理系统的示例代码,包括添加、删除、修改和查询学生信息的功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class StudentInfoManagementSystem extends JFrame implements ActionListener {
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JTextField textField1;
private JTextField textField2;
private JTextField textField3;
private JTextField textField4;
private JButton addButton;
private JButton deleteButton;
private JButton modifyButton;
private JButton queryButton;
private JTable table;
private DefaultTableModel model;
private JScrollPane scrollPane;
public StudentInfoManagementSystem() {
super("学生信息管理系统");
this.setSize(500, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
label1 = new JLabel("学号:");
label2 = new JLabel("姓名:");
label3 = new JLabel("性别:");
label4 = new JLabel("年龄:");
textField1 = new JTextField(10);
textField2 = new JTextField(10);
textField3 = new JTextField(10);
textField4 = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
modifyButton = new JButton("修改");
queryButton = new JButton("查询");
addButton.addActionListener(this);
deleteButton.addActionListener(this);
modifyButton.addActionListener(this);
queryButton.addActionListener(this);
panel1.setLayout(new GridLayout(4, 2));
panel1.add(label1);
panel1.add(textField1);
panel1.add(label2);
panel1.add(textField2);
panel1.add(label3);
panel1.add(textField3);
panel1.add(label4);
panel1.add(textField4);
panel2.add(addButton);
panel2.add(deleteButton);
panel2.add(modifyButton);
panel2.add(queryButton);
model = new DefaultTableModel();
model.addColumn("学号");
model.addColumn("姓名");
model.addColumn("性别");
model.addColumn("年龄");
table = new JTable(model);
scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(450, 300));
panel3.add(scrollPane);
this.add(panel1, BorderLayout.NORTH);
this.add(panel2, BorderLayout.CENTER);
this.add(panel3, BorderLayout.SOUTH);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String[] data = new String[4];
data[0] = textField1.getText();
data[1] = textField2.getText();
data[2] = textField3.getText();
data[3] = textField4.getText();
model.addRow(data);
} else if (e.getSource() == deleteButton) {
int row = table.getSelectedRow();
if (row != -1) {
model.removeRow(row);
}
} else if (e.getSource() == modifyButton) {
int row = table.getSelectedRow();
if (row != -1) {
model.setValueAt(textField1.getText(), row, 0);
model.setValueAt(textField2.getText(), row, 1);
model.setValueAt(textField3.getText(), row, 2);
model.setValueAt(textField4.getText(), row, 3);
}
} else if (e.getSource() == queryButton) {
String id = textField1.getText();
for (int i = 0; i < model.getRowCount(); i++) {
if (model.getValueAt(i, 0).equals(id)) {
table.setRowSelectionInterval(i, i);
return;
}
}
JOptionPane.showMessageDialog(null, "没有找到该学生!");
}
}
public static void main(String[] args) {
new StudentInfoManagementSystem();
}
}
```
这个程序使用了JPanel、JLabel、JTextField、JButton、JTable和JScrollPane等Swing组件。用户可以在文本框中输入学生信息,然后点击相应的按钮来执行添加、删除、修改或查询操作。学生信息以表格的形式展示在程序的下方。
阅读全文