!a! 2023-04-14 13:21 采纳率: 100%
浏览 116
已结题

用java编写注册界面

img

img


一、 用Java编写
二、 做到和要求一致
三、 界面的设计可以略有差异

  • 写回答

5条回答 默认 最新

  • 语言-逆行者 2023-04-14 13:41
    关注

    基于new BIng和ChatGPT加以修改后的编写:

    img

    img

    
    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();
        }
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月14日
  • 已采纳回答 4月14日
  • 提问应符合社区要求 4月14日
  • 创建了问题 4月14日

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题