📄 add.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
class Add extends JFrame {
private JLabel lab1, lab2, lab3, lab4, lab5;
private JTextField num, name, sex, age, remark;
private JButton save, next, rebound;
private Container container;
static int count = 0;
public Add() throws Exception {
super("学生信息录入");
final File file = new File("E:\\info.txt");
final FileOutputStream bos = new FileOutputStream(file);
final ObjectOutputStream oos = new ObjectOutputStream(bos);
container = getContentPane();
container.setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jp = new JPanel();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
jp.setLayout(new GridLayout(5,1));
jp1.setLayout(new GridLayout(5,1));
jp2.setLayout(new FlowLayout());
container.add("West",jp);
container.add("Center",jp1);
container.add("South",jp2);
lab1 = new JLabel("学号:");
lab2 = new JLabel("姓名:");
lab3 = new JLabel("性别:");
lab4 = new JLabel("年龄:");
lab5 = new JLabel("备注:");
num = new JTextField(18);
name = new JTextField(12);
sex = new JTextField(2);
age = new JTextField(3);
remark = new JTextField(30);
save = new JButton("保存");
next = new JButton("下一个");
rebound = new JButton("返回");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
oos.writeObject(new Student(num.getText(), name.getText(),
sex.getText(), age.getText(), remark.getText()));
count++;
oos.flush();
} catch (Exception event) {
System.out.println("写入文件时发生异常");
}
}
});
next.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num.setText("");
name.setText("");
sex.setText("");
age.setText("");
remark.setText("");
}
});
rebound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
oos.close();
dispose();
} catch (IOException evem) {
System.out.println("文件关闭异常");
}
}
});
jp.add(lab1);
jp1.add(num);
jp.add(lab2);
jp1.add(name);
jp.add(lab3);
jp1.add(sex);
jp.add(lab4);
jp1.add(age);
jp.add(lab5);
jp1.add(remark);
jp2.add(save);
jp2.add(next);
jp2.add(rebound);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -