一、 用Java编写
二、 做到和要求一致
三、 界面的设计可以略有差异
基于new BIng和ChatGPT加以修改后的编写:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class one extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JLabel labelAccount, labelPassword, labelConfirmPassword, labelPhoneNumber, labelGender, labelBirthday;
private JTextField textFieldAccount, textFieldPhoneNumber;
private JPasswordField passwordField, confirmPasswordField;
private JComboBox<String> comboBoxYear, comboBoxMonth, comboBoxDay;
private JRadioButton radioButtonMale, radioButtonFemale;
private JButton buttonRegister, buttonReset;
public one() {
setTitle("注册");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(10, 10, 10, 10);
labelAccount = new JLabel("账号:");
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(labelAccount, constraints);
textFieldAccount = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 0;
panel.add(textFieldAccount, constraints);
labelPassword = new JLabel("密码:");
constraints.gridx = 0;
constraints.gridy = 1;
panel.add(labelPassword, constraints);
passwordField = new JPasswordField(20);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(passwordField, constraints);
labelConfirmPassword = new JLabel("确认密码:");
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(labelConfirmPassword, constraints);
confirmPasswordField = new JPasswordField(20);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(confirmPasswordField, constraints);
labelPhoneNumber = new JLabel("手机号码:");
constraints.gridx = 0;
constraints.gridy = 3;
panel.add(labelPhoneNumber, constraints);
textFieldPhoneNumber = new JTextField(20);
constraints.gridx = 1;
constraints.gridy = 3;
panel.add(textFieldPhoneNumber, constraints);
labelGender = new JLabel("性别:");
constraints.gridx = 0;
constraints.gridy = 4;
panel.add(labelGender, constraints);
radioButtonMale = new JRadioButton("男");
constraints.gridx = 1;
constraints.gridy = 4;
panel.add(radioButtonMale, constraints);
radioButtonFemale = new JRadioButton("女");
constraints.gridx = 2;
constraints.gridy = 4;
panel.add(radioButtonFemale, constraints);
ButtonGroup buttonGroupGender = new ButtonGroup();
buttonGroupGender.add(radioButtonMale);
buttonGroupGender.add(radioButtonFemale);
labelBirthday = new JLabel("生日:");
constraints.gridx = 0;
constraints.gridy = 5;
panel.add(labelBirthday, constraints);
comboBoxYear = new JComboBox<String>(new String[]{"年份", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000"});
constraints.gridx = 1;
constraints.gridy = 5;
panel.add(comboBoxYear, constraints);
comboBoxMonth = new JComboBox<String>(new String[]{"月份", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"});
constraints.gridx = 2;
constraints.gridy = 5;
panel.add(comboBoxMonth, constraints);
comboBoxDay = new JComboBox<String>(new String[]{"日期", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"});
constraints.gridx = 3;
constraints.gridy = 5;
panel.add(comboBoxDay, constraints);
buttonRegister = new JButton("注册");
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 2;
panel.add(buttonRegister, constraints);
buttonReset = new JButton("重置");
constraints.gridx = 2;
constraints.gridy = 6;
constraints.gridwidth = 2;
panel.add(buttonReset, constraints);
add(panel);
setVisible(true);
buttonRegister.addActionListener(this);
buttonReset.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == buttonRegister) {
String account = textFieldAccount.getText();
char[] password = passwordField.getPassword();
char[] confirmPassword = confirmPasswordField.getPassword();
String phoneNumber = textFieldPhoneNumber.getText();
String gender = "";
if (radioButtonMale.isSelected()) {
gender = "男";
} else if (radioButtonFemale.isSelected()) {
gender = "女";
}
String year = (String) comboBoxYear.getSelectedItem();
String month = (String) comboBoxMonth.getSelectedItem();
String day = (String) comboBoxDay.getSelectedItem();
if (account.equals("") || password.length == 0 || confirmPassword.length == 0 || phoneNumber.equals("") || gender.equals("") || year.equals("年份") || month.equals("月份") || day.equals("日期")) {
JOptionPane.showMessageDialog(this, "请完整填写注册信息!");
} else if (!String.valueOf(password).equals(String.valueOf(confirmPassword))) {
JOptionPane.showMessageDialog(this, "两次输入的密码不一致,请重新输入!");
passwordField.setText("");
confirmPasswordField.setText("");
} else {
try {
FileWriter fileWriter = new FileWriter("user.txt", true);
fileWriter.write("用户名:"+account + ",密码:" + String.valueOf(password) + ",性别:"+ gender + ",手机号:"+ phoneNumber + ",生日:" + year + "年" + month + "月" + day + "日\n");
fileWriter.close();
JOptionPane.showMessageDialog(this, "注册成功!");
textFieldAccount.setText("");
passwordField.setText("");
confirmPasswordField.setText("");
textFieldPhoneNumber.setText("");
radioButtonMale.setSelected(false);
radioButtonFemale.setSelected(false);
comboBoxYear.setSelectedIndex(0);
comboBoxMonth.setSelectedIndex(0);
comboBoxDay.setSelectedIndex(0);
} catch (IOException e) {
e.printStackTrace();
}
}
} else if (event.getSource() == buttonReset) {
textFieldAccount.setText("");
passwordField.setText("");
confirmPasswordField.setText("");
textFieldPhoneNumber.setText("");
radioButtonMale.setSelected(false);
radioButtonFemale.setSelected(false);
comboBoxYear.setSelectedIndex(0);
comboBoxMonth.setSelectedIndex(0);
comboBoxDay.setSelectedIndex(0);
}
}
public static void main(String[] args) {
new one();
}
}