定义一个Student类,编写程序使用对象输出流将一个Student对象和一个字符串对象写入student.dat文件中,然后使用对象输入流读出对象。 备注:利用Swing实现图形用户界面,在图形用户界面上完成上述的输入与输出显示到图形用户界面。
时间: 2024-02-25 13:55:37 浏览: 79
编写一个JAVA程序,定义一个Student类
5星 · 资源好评率100%
好的,我会尽力回答你的问题。
首先,我们需要定义一个 `Student` 类,代码如下:
```java
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
private String gender;
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String toString() {
return "Name: " + name + ", Age: " + age + ", Gender: " + gender;
}
}
```
上述代码定义了一个 `Student` 类,包含了学生的姓名、年龄和性别,并实现了 `Serializable` 接口,以便通过对象流进行序列化和反序列化。
接下来,我们需要实现图形用户界面,可以使用 Java Swing 来实现。代码如下:
```java
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class StudentGUI extends JFrame implements ActionListener {
private JTextField nameField, ageField, genderField;
private JButton saveButton, loadButton;
public StudentGUI() {
super("Student Information");
// Create UI components
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField(20);
JLabel ageLabel = new JLabel("Age:");
ageField = new JTextField(3);
JLabel genderLabel = new JLabel("Gender:");
genderField = new JTextField(10);
JPanel inputPanel = new JPanel(new FlowLayout());
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(ageLabel);
inputPanel.add(ageField);
inputPanel.add(genderLabel);
inputPanel.add(genderField);
saveButton = new JButton("Save");
saveButton.addActionListener(this);
loadButton = new JButton("Load");
loadButton.addActionListener(this);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(saveButton);
buttonPanel.add(loadButton);
Container contentPane = getContentPane();
contentPane.add(inputPanel, "North");
contentPane.add(buttonPanel, "South");
// Set window properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 150);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == saveButton) {
saveStudent();
} else if (event.getSource() == loadButton) {
loadStudent();
}
}
private void saveStudent() {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String gender = genderField.getText();
Student student = new Student(name, age, gender);
try {
FileOutputStream fileOut = new FileOutputStream("student.dat");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(student);
objectOut.writeObject("This is a string object.");
objectOut.close();
JOptionPane.showMessageDialog(this, "Student information has been saved.");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
private void loadStudent() {
try {
FileInputStream fileIn = new FileInputStream("student.dat");
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Student student = (Student) objectIn.readObject();
String stringObject = (String) objectIn.readObject();
objectIn.close();
JOptionPane.showMessageDialog(this, student.toString() + "\n" + stringObject);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage());
}
}
public static void main(String[] args) {
new StudentGUI();
}
}
```
上述代码实现了一个图形用户界面,包含了一个输入面板和一个按钮面板。用户可以在输入面板中输入学生的信息,并通过保存按钮将学生对象和一个字符串对象写入 `student.dat` 文件中。通过加载按钮,我们可以将这些对象从文件中读出并在消息框中显示出来。
在 `saveStudent` 方法中,我们首先从输入面板中获取用户输入的学生信息,然后创建一个 `Student` 对象,将它和一个字符串对象一起写入 `student.dat` 文件中。在 `loadStudent` 方法中,我们从 `student.dat` 文件中读出 `Student` 对象和字符串对象,并在消息框中显示出来。
这样,我们就实现了一个利用图形用户界面和对象流进行输入和输出的程序。
阅读全文