java学生信息管理系统用窗口实现代码
时间: 2023-12-11 08:14:13 浏览: 61
以下是一个简单的Java学生信息管理系统窗口实现的代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class StudentManager extends JFrame implements ActionListener {
private JTextField idField, nameField, ageField;
private JButton addButton, deleteButton, updateButton, searchButton;
private JTable table;
private DefaultTableModel tableModel;
public StudentManager() {
setTitle("学生信息管理系统");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建表格模型和表格
tableModel = new DefaultTableModel();
tableModel.addColumn("学号");
tableModel.addColumn("姓名");
tableModel.addColumn("年龄");
table = new JTable(tableModel);
// 创建文本框和按钮
idField = new JTextField(10);
nameField = new JTextField(10);
ageField = new JTextField(10);
addButton = new JButton("添加");
deleteButton = new JButton("删除");
updateButton = new JButton("修改");
searchButton = new JButton("查找");
// 添加控件到窗口
JPanel panel = new JPanel(new GridLayout(1, 5));
panel.add(idField);
panel.add(nameField);
panel.add(ageField);
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(searchButton);
add(panel, BorderLayout.SOUTH);
add(new JScrollPane(table), BorderLayout.CENTER);
// 注册事件监听器
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
searchButton.addActionListener(this);
}
// 处理按钮点击事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String[] rowData = { idField.getText(), nameField.getText(), ageField.getText() };
tableModel.addRow(rowData);
} else if (e.getSource() == deleteButton) {
int[] selectedRows = table.getSelectedRows();
for (int i = selectedRows.length - 1; i >= 0; i--) {
tableModel.removeRow(selectedRows[i]);
}
} else if (e.getSource() == updateButton) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
tableModel.setValueAt(idField.getText(), selectedRow, 0);
tableModel.setValueAt(nameField.getText(), selectedRow, 1);
tableModel.setValueAt(ageField.getText(), selectedRow, 2);
}
} else if (e.getSource() == searchButton) {
String id = idField.getText();
for (int i = 0; i < tableModel.getRowCount(); i++) {
String curId = (String) tableModel.getValueAt(i, 0);
if (id.equals(curId)) {
table.setRowSelectionInterval(i, i);
return;
}
}
JOptionPane.showMessageDialog(this, "未找到该学生");
}
}
public static void main(String[] args) {
StudentManager sm = new StudentManager();
sm.setVisible(true);
}
}
```
该程序使用了Java Swing类库,实现了一个简单的学生信息管理系统。用户可以通过窗口中的文本框和按钮添加、删除、修改和查找学生信息。同时,程序使用了JTable类来展示学生信息,使用了DefaultTableModel类来管理表格数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)