java声明StudentJFrame类写一个Student对象信息管理
时间: 2023-07-19 15:39:01 浏览: 100
Java创建一个类-类名称student
好的,我可以帮你写一个简单的Java程序来管理Student对象的信息。首先,我们需要声明一个名为StudentJFrame的类来设计界面,然后在其中添加相应的组件来实现增加、删除、查找和修改学生信息的功能。接下来,我们可以在主函数中创建一个Student对象数组,用来存储学生对象的信息。
以下是一个简单的实现示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class StudentJFrame extends JFrame implements ActionListener {
private JLabel label1, label2, label3, label4;
private JTextField text1, text2, text3, text4;
private JButton btnAdd, btnDelete, btnSearch, btnUpdate;
private JPanel panel1, panel2;
private Student[] students = new Student[100];
private int num = 0;
public StudentJFrame() {
super("Student Information Management");
label1 = new JLabel("学号");
label2 = new JLabel("姓名");
label3 = new JLabel("性别");
label4 = new JLabel("年龄");
text1 = new JTextField(10);
text2 = new JTextField(10);
text3 = new JTextField(10);
text4 = new JTextField(10);
btnAdd = new JButton("添加");
btnDelete = new JButton("删除");
btnSearch = new JButton("查找");
btnUpdate = new JButton("修改");
panel1 = new JPanel();
panel1.setLayout(new GridLayout(4, 2));
panel1.add(label1);
panel1.add(text1);
panel1.add(label2);
panel1.add(text2);
panel1.add(label3);
panel1.add(text3);
panel1.add(label4);
panel1.add(text4);
panel2 = new JPanel();
panel2.setLayout(new GridLayout(1, 4));
panel2.add(btnAdd);
panel2.add(btnDelete);
panel2.add(btnSearch);
panel2.add(btnUpdate);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);
btnAdd.addActionListener(this);
btnDelete.addActionListener(this);
btnSearch.addActionListener(this);
btnUpdate.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd) {
// 添加学生信息
students[num] = new Student(text1.getText(), text2.getText(), text3.getText(), Integer.parseInt(text4.getText()));
num++;
JOptionPane.showMessageDialog(this, "添加成功!");
} else if (e.getSource() == btnDelete) {
// 删除学生信息
String id = JOptionPane.showInputDialog(this, "请输入要删除的学生学号:");
boolean flag = false;
for (int i = 0; i < num; i++) {
if (students[i].getId().equals(id)) {
for (int j = i; j < num - 1; j++) {
students[j] = students[j + 1];
}
num--;
flag = true;
JOptionPane.showMessageDialog(this, "删除成功!");
break;
}
}
if (!flag) {
JOptionPane.showMessageDialog(this, "未找到该学生记录!");
}
} else if (e.getSource() == btnSearch) {
// 查找学生信息
String id = JOptionPane.showInputDialog(this, "请输入要查找的学生学号:");
boolean flag = false;
for (int i = 0; i < num; i++) {
if (students[i].getId().equals(id)) {
text1.setText(students[i].getId());
text2.setText(students[i].getName());
text3.setText(students[i].getGender());
text4.setText(String.valueOf(students[i].getAge()));
flag = true;
JOptionPane.showMessageDialog(this, "查找成功!");
break;
}
}
if (!flag) {
JOptionPane.showMessageDialog(this, "未找到该学生记录!");
}
} else if (e.getSource() == btnUpdate) {
// 修改学生信息
String id = JOptionPane.showInputDialog(this, "请输入要修改的学生学号:");
boolean flag = false;
for (int i = 0; i < num; i++) {
if (students[i].getId().equals(id)) {
students[i].setName(text2.getText());
students[i].setGender(text3.getText());
students[i].setAge(Integer.parseInt(text4.getText()));
flag = true;
JOptionPane.showMessageDialog(this, "修改成功!");
break;
}
}
if (!flag) {
JOptionPane.showMessageDialog(this, "未找到该学生记录!");
}
}
}
public static void main(String[] args) {
new StudentJFrame();
}
}
class Student {
private String id;
private String name;
private String gender;
private int age;
public Student(String id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAge(int age) {
this.age = age;
}
}
```
以上代码实现了一个简单的学生信息管理系统,可以通过界面添加、删除、查找和修改学生信息。注意:这只是一个简单的实现示例,完整的学生信息管理系统需要考虑更多的情况,比如数据存储、数据校验、界面美化等。
阅读全文