帮我写出java图形化界面学生信息管理的代码
时间: 2024-03-13 12:46:25 浏览: 23
好的,我可以提供一个简单的Java图形化界面学生信息管理的代码示例,供您参考:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class StudentManagementSystem extends JFrame implements ActionListener {
private JLabel title, nameLabel, idLabel, majorLabel;
private JTextField nameField, idField, majorField;
private JButton addButton, deleteButton, updateButton, clearButton;
private JTable table;
private DefaultTableModel model;
private ArrayList<Student> studentList;
public StudentManagementSystem() {
super("Student Management System");
// Initialize components
title = new JLabel("Student Management System");
title.setFont(new Font("Arial", Font.BOLD, 24));
nameLabel = new JLabel("Name:");
idLabel = new JLabel("ID:");
majorLabel = new JLabel("Major:");
nameField = new JTextField(20);
idField = new JTextField(20);
majorField = new JTextField(20);
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
updateButton = new JButton("Update");
clearButton = new JButton("Clear");
// Add components to panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(title);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(nameLabel);
panel.add(nameField);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(idLabel);
panel.add(idField);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(majorLabel);
panel.add(majorField);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(addButton);
panel.add(deleteButton);
panel.add(updateButton);
panel.add(clearButton);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
// Initialize table
String[] columnNames = {"Name", "ID", "Major"};
model = new DefaultTableModel(columnNames, 0);
table = new JTable(model);
// Add components to frame
JScrollPane scrollPane = new JScrollPane(table);
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(scrollPane, BorderLayout.CENTER);
// Add action listeners
addButton.addActionListener(this);
deleteButton.addActionListener(this);
updateButton.addActionListener(this);
clearButton.addActionListener(this);
// Initialize student list
studentList = new ArrayList<Student>();
// Set frame properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addButton) {
String name = nameField.getText();
String id = idField.getText();
String major = majorField.getText();
Student student = new Student(name, id, major);
studentList.add(student);
Object[] rowData = {student.getName(), student.getId(), student.getMajor()};
model.addRow(rowData);
} else if (e.getSource() == deleteButton) {
int rowIndex = table.getSelectedRow();
if (rowIndex >= 0) {
studentList.remove(rowIndex);
model.removeRow(rowIndex);
}
} else if (e.getSource() == updateButton) {
int rowIndex = table.getSelectedRow();
if (rowIndex >= 0) {
String name = nameField.getText();
String id = idField.getText();
String major = majorField.getText();
Student student = studentList.get(rowIndex);
student.setName(name);
student.setId(id);
student.setMajor(major);
model.setValueAt(name, rowIndex, 0);
model.setValueAt(id, rowIndex, 1);
model.setValueAt(major, rowIndex, 2);
}
} else if (e.getSource() == clearButton) {
nameField.setText("");
idField.setText("");
majorField.setText("");
}
}
public static void main(String[] args) {
new StudentManagementSystem();
}
private class Student {
private String name;
private String id;
private String major;
public Student(String name, String id, String major) {
this.name = name;
this.id = id;
this.major = major;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
}
}
```
这个代码中,我们使用了JFrame、JLabel、JTextField、JButton、JTable等Swing组件来实现图形化界面,同时使用了ArrayList和DefaultTableModel来存储和展示学生信息。可以根据自己的需求进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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_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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)