应用Java开发一个成绩管理模块。该模块能够完成对成绩的录入、删除和查询功能能并建立一个窗口能与用户进行交互代码
时间: 2024-03-05 18:51:56 浏览: 27
好的,以下是一个简单的Java成绩管理模块的代码示例,包括UI界面和成绩管理功能的实现:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class ScoreManager {
private JFrame frame;
private JPanel panel;
private JTextField nameField, scoreField;
private JTextArea resultArea;
private JButton addButton, deleteButton, queryButton;
private ArrayList<Student> students;
public ScoreManager() {
// 初始化UI界面
frame = new JFrame("成绩管理");
panel = new JPanel(new GridLayout(4, 2, 10, 10));
frame.add(panel, BorderLayout.CENTER);
panel.add(new JLabel("姓名"));
nameField = new JTextField();
panel.add(nameField);
panel.add(new JLabel("成绩"));
scoreField = new JTextField();
panel.add(scoreField);
addButton = new JButton("录入");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addStudent();
}
});
panel.add(addButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteStudent();
}
});
panel.add(deleteButton);
queryButton = new JButton("查询");
queryButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
queryStudent();
}
});
panel.add(queryButton);
resultArea = new JTextArea();
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
frame.add(scrollPane, BorderLayout.SOUTH);
frame.setSize(400, 300);
frame.setVisible(true);
// 初始化学生列表
students = new ArrayList<Student>();
}
private void addStudent() {
String name = nameField.getText();
String scoreString = scoreField.getText();
if (name.isEmpty() || scoreString.isEmpty()) {
JOptionPane.showMessageDialog(frame, "姓名和成绩不能为空", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
int score = 0;
try {
score = Integer.parseInt(scoreString);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "成绩必须是整数", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
Student student = new Student(name, score);
students.add(student);
nameField.setText("");
scoreField.setText("");
}
private void deleteStudent() {
String name = nameField.getText();
if (name.isEmpty()) {
JOptionPane.showMessageDialog(frame, "请输入要删除的学生姓名", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
boolean found = false;
for (Student student : students) {
if (student.getName().equals(name)) {
students.remove(student);
found = true;
break;
}
}
if (!found) {
JOptionPane.showMessageDialog(frame, "未找到该学生", "错误", JOptionPane.ERROR_MESSAGE);
}
nameField.setText("");
scoreField.setText("");
}
private void queryStudent() {
String name = nameField.getText();
String scoreString = scoreField.getText();
resultArea.setText("");
if (!name.isEmpty()) {
for (Student student : students) {
if (student.getName().equals(name)) {
resultArea.append(student.toString() + "\n");
}
}
} else if (!scoreString.isEmpty()) {
int score = 0;
try {
score = Integer.parseInt(scoreString);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "成绩必须是整数", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
for (Student student : students) {
if (student.getScore() == score) {
resultArea.append(student.toString() + "\n");
}
}
} else {
JOptionPane.showMessageDialog(frame, "请输入要查询的学生姓名或成绩", "错误", JOptionPane.ERROR_MESSAGE);
}
nameField.setText("");
scoreField.setText("");
}
public static void main(String[] args) {
ScoreManager manager = new ScoreManager();
}
}
class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public String toString() {
return name + " " + score;
}
}
```
在这个示例代码中,我们使用了Java Swing库来构建UI界面,使用了ArrayList来存储学生的成绩信息。在UI界面中,我们使用了JTextField、JButton和JTextArea等组件来实现录入、删除和查询功能。在成绩管理功能中,我们使用了ArrayList的add()、remove()和get()方法来实现对学生信息的增删查操作。
希望这个示例代码可以帮助您实现一个Java成绩管理模块,并建立一个窗口与用户进行交互。如果您有任何问题,请随时问我。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)