netbeans实现一个界面包含,姓名,密码,密码确认这些输入行,和可以勾选的个人爱好,阅读,健身,游泳,美食,要求实现功能验证密码是否一致

netbeans实现一个界面包含,姓名,密码,密码确认这些输入行,和可以勾选的个人爱好,阅读,健身,游泳,美食,要求实现功能验证密码是否一致_第1张图片
netbeans实现一个界面包含,姓名,密码,密码确认这些输入行,和可以勾选的个人爱好,阅读,健身,游泳,美食,要求实现功能验证密码是否一致_第2张图片
code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package registrationform;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegistrationForm extends JFrame {
    private JTextField nameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JCheckBox readingCheckBox;
    private JCheckBox fitnessCheckBox;
    private JCheckBox swimmingCheckBox;
    private JCheckBox foodCheckBox;
    private JButton submitButton;

    public RegistrationForm() {
        // 设置窗口标题
        setTitle("Registration Form");

        // 创建组件
        JLabel nameLabel = new JLabel("Name:");
        nameField = new JTextField(20);

        JLabel passwordLabel = new JLabel("Password:");
        passwordField = new JPasswordField(20);

        JLabel confirmPasswordLabel = new JLabel("Confirm Password:");
        confirmPasswordField = new JPasswordField(20);

        JLabel hobbiesLabel = new JLabel("Hobbies:");
        readingCheckBox = new JCheckBox("Reading");
        fitnessCheckBox = new JCheckBox("Fitness");
        swimmingCheckBox = new JCheckBox("Swimming");
        foodCheckBox = new JCheckBox("Food");

        submitButton = new JButton("Submit");

        // 创建容器
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.WEST;
        constraints.insets = new Insets(5, 5, 5, 5);

        // 添加姓名标签和输入框
        constraints.gridx = 0;
        constraints.gridy = 0;
        panel.add(nameLabel, constraints);

        constraints.gridx = 1;
        panel.add(nameField, constraints);

        // 添加密码标签和输入框
        constraints.gridx = 0;
        constraints.gridy = 1;
        panel.add(passwordLabel, constraints);

        constraints.gridx = 1;
        panel.add(passwordField, constraints);

        // 添加确认密码标签和输入框
        constraints.gridx = 0;
        constraints.gridy = 2;
        panel.add(confirmPasswordLabel, constraints);

        constraints.gridx = 1;
        panel.add(confirmPasswordField, constraints);

        // 添加爱好标签和复选框
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 2;
        panel.add(hobbiesLabel, constraints);

        constraints.gridy = 4;
        constraints.gridwidth = 1;
        panel.add(readingCheckBox, constraints);

        constraints.gridy = 5;
        panel.add(fitnessCheckBox, constraints);

        constraints.gridy = 6;
        panel.add(swimmingCheckBox, constraints);

        constraints.gridy = 7;
        panel.add(foodCheckBox, constraints);

        // 添加提交按钮
        constraints.gridx = 0;
        constraints.gridy = 8;
        constraints.gridwidth = 2;
        constraints.anchor = GridBagConstraints.CENTER;
        panel.add(submitButton, constraints);

        // 添加事件处理程序
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText();
                char[] password = passwordField.getPassword();
                char[] confirmPassword = confirmPasswordField.getPassword();

                // 验证密码是否一致
                if (passwordMatch(password, confirmPassword)) {
                    String hobbies = "Hobbies: ";
                    if (readingCheckBox.isSelected())
                        hobbies += "Reading";
                    if (fitnessCheckBox.isSelected())
                        hobbies += "Fitness ";
                    if (swimmingCheckBox.isSelected())
                        hobbies += "Swimming ";
                    if (foodCheckBox.isSelected())
                        hobbies += "Food";

                    JOptionPane.showMessageDialog(RegistrationForm.this,
                            "Registration successful!\n\n" +
                                    "Name: " + name +"Password: " + String.valueOf(password) + "\n" +
                                    hobbies);
                } else {
                    JOptionPane.showMessageDialog(RegistrationForm.this,
                            "Passwords do not match!\n\n" +
                                    "Entered Password:\n" + String.valueOf(password),
                            "Error",
                            JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        // 将容器添加到窗口
        add(panel);

        // 设置窗口属性
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null); // 将窗口居中显示
    }

    // 验证密码是否一致
    private boolean passwordMatch(char[] password, char[] confirmPassword) {
        if (password.length != confirmPassword.length)
            return false;

        for (int i = 0; i < password.length; i++) {
            if (password[i] != confirmPassword[i])
                return false;
        }

        return true;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                RegistrationForm form = new RegistrationForm();
                form.setVisible(true);
            }
        });
    }
}

你可能感兴趣的:(安农netbeans,java,jvm)