2、请设计一个个人信息注册界面,要求界面中至少包含有文本输入框(输入学号、姓名等内容)、单选按钮(性别)和复选按钮(用于勾选个人兴趣爱好)和【提交】按钮,要求布局要求合理、美观。要求实现如下功能: (Exp6_02.java) (1)当在文本框等控件输入信息后,按下【提交】按钮,判断所填信息是否完整,如是则将个人所填和所选信息内容组合成一个字符串(信息项之间用#分隔,组合样例:20220001#张三#男#足球、游泳、看电影)写入文件stuInfo.txt中。 (2)打开记事本,看看所注册信息是否已存入文件中(用Java)
时间: 2024-01-24 08:02:07 浏览: 79
好的,以下是一个 Java 实现个人信息注册界面的示例代码,按照您的要求进行设计:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class RegisterWindow extends JFrame implements ActionListener {
private JLabel idLabel, nameLabel, genderLabel, hobbyLabel;
private JTextField idField, nameField;
private JRadioButton maleRadioButton, femaleRadioButton;
private JCheckBox footballCheckBox, basketballCheckBox, swimmingCheckBox;
private JButton submitButton;
public RegisterWindow() {
setTitle("个人信息注册");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2));
idLabel = new JLabel("学号:");
idField = new JTextField();
nameLabel = new JLabel("姓名:");
nameField = new JTextField();
genderLabel = new JLabel("性别:");
maleRadioButton = new JRadioButton("男");
femaleRadioButton = new JRadioButton("女");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleRadioButton);
genderGroup.add(femaleRadioButton);
hobbyLabel = new JLabel("兴趣爱好:");
footballCheckBox = new JCheckBox("足球");
basketballCheckBox = new JCheckBox("篮球");
swimmingCheckBox = new JCheckBox("游泳");
submitButton = new JButton("提交");
submitButton.addActionListener(this);
panel.add(idLabel);
panel.add(idField);
panel.add(nameLabel);
panel.add(nameField);
panel.add(genderLabel);
panel.add(maleRadioButton);
panel.add(new JLabel(""));
panel.add(femaleRadioButton);
panel.add(hobbyLabel);
panel.add(footballCheckBox);
panel.add(new JLabel(""));
panel.add(basketballCheckBox);
panel.add(new JLabel(""));
panel.add(swimmingCheckBox);
panel.add(submitButton);
add(panel);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
String id = idField.getText();
String name = nameField.getText();
String gender = maleRadioButton.isSelected() ? "男" : "女";
String hobby = "";
if (footballCheckBox.isSelected()) {
hobby += "足球、";
}
if (basketballCheckBox.isSelected()) {
hobby += "篮球、";
}
if (swimmingCheckBox.isSelected()) {
hobby += "游泳、";
}
if (!id.equals("") && !name.equals("") && !hobby.equals("")) {
String info = id + "#" + name + "#" + gender + "#" + hobby.substring(0, hobby.length() - 1);
try {
FileWriter fw = new FileWriter("stuInfo.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(info);
bw.newLine();
bw.close();
fw.close();
JOptionPane.showMessageDialog(null, "注册成功");
} catch (IOException ioe) {
JOptionPane.showMessageDialog(null, "文件写入失败");
}
} else {
JOptionPane.showMessageDialog(null, "请填写完整信息");
}
}
}
public static void main(String[] args) {
new RegisterWindow();
}
}
```
这个示例代码使用了 Java Swing 组件来实现个人信息注册界面,包括文本框、单选按钮、复选按钮、标签、按钮等。在提交按钮的监听方法中,会判断所填信息是否完整,如果是,则将个人所填和所选信息内容组合成一个字符串,写入到文件 stuInfo.txt 中。如果文件写入成功,则弹出注册成功的提示对话框;否则弹出文件写入失败的提示对话框。
阅读全文