java编写学生管理系统带界面
时间: 2023-10-17 16:57:18 浏览: 56
好的,以下是一个基于Java Swing的学生管理系统界面实现示例:
```
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class StudentManagementSystemGUI {
private JFrame frame;
private JTextField nameField;
private JTextField ageField;
private JTable table;
private DefaultTableModel tableModel;
private Student[] students = new Student[10];
private int numStudents = 0;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
StudentManagementSystemGUI window = new StudentManagementSystemGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public StudentManagementSystemGUI() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 640, 480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel titleLabel = new JLabel("学生管理系统");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
titleLabel.setBounds(240, 20, 160, 40);
frame.getContentPane().add(titleLabel);
JPanel addPanel = new JPanel();
addPanel.setBounds(20, 80, 240, 200);
addPanel.setLayout(null);
addPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("添加学生"));
frame.getContentPane().add(addPanel);
JLabel nameLabel = new JLabel("姓名:");
nameLabel.setBounds(20, 40, 40, 20);
addPanel.add(nameLabel);
nameField = new JTextField();
nameField.setBounds(60, 40, 120, 20);
addPanel.add(nameField);
JLabel ageLabel = new JLabel("年龄:");
ageLabel.setBounds(20, 80, 40, 20);
addPanel.add(ageLabel);
ageField = new JTextField();
ageField.setBounds(60, 80, 120, 20);
addPanel.add(ageField);
JButton addButton = new JButton("添加");
addButton.setBounds(80, 120, 80, 20);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
if (numStudents < students.length) {
Student student = new Student(name, age);
students[numStudents] = student;
numStudents++;
JOptionPane.showMessageDialog(null, "添加学生成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
nameField.setText("");
ageField.setText("");
refreshTable();
} else {
JOptionPane.showMessageDialog(null, "学生列表已满,无法添加新学生!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
addPanel.add(addButton);
JPanel tablePanel = new JPanel();
tablePanel.setBounds(280, 80, 320, 360);
tablePanel.setLayout(null);
tablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("学生列表"));
frame.getContentPane().add(tablePanel);
String[] header = {"姓名", "年龄"};
tableModel = new DefaultTableModel(null, header);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(20, 40, 280, 300);
tablePanel.add(scrollPane);
refreshTable();
JButton deleteButton = new JButton("删除");
deleteButton.setBounds(220, 360, 80, 20);
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(null, "请选择要删除的学生!", "错误", JOptionPane.ERROR_MESSAGE);
} else {
int confirm = JOptionPane.showConfirmDialog(null, "确定要删除该学生吗?", "确认删除", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
for (int i = row; i < numStudents - 1; i++) {
students[i] = students[i + 1];
}
numStudents--;
JOptionPane.showMessageDialog(null, "删除学生成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
refreshTable();
}
}
}
});
tablePanel.add(deleteButton);
JButton exitButton = new JButton("退出");
exitButton.setBounds(520, 420, 80, 20);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showConfirmDialog(null, "确定要退出程序吗?", "确认退出", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
System.exit(0);
}
}
});
frame.getContentPane().add(exitButton);
}
private void refreshTable() {
tableModel.setRowCount(0);
for (int i = 0; i < numStudents; i++) {
Object[] row = {students[i].getName(), students[i].getAge()};
tableModel.addRow(row);
}
}
}
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
```
这个程序中,通过Java Swing的各种GUI组件来实现学生管理系统的界面,包括添加学生、删除学生、显示学生列表以及退出系统。同时,定义了一个Student类来存储学生信息,并通过DefaultTableModel和JTable来显示学生列表。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.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)