java--用户注册界面的实现

13 篇文章 1 订阅
  • 实验目的

(1)掌握Swing常见的布局管理器。

(2)掌握文本框的用法。

(3)掌握密码框的用法。

  • 实验内容

(1)掌握Java图形界面设计。

(2)了解其中的相关概念、类的使用。

(3)编写RegistDemo类,创建窗体实现用户注册功能。

  • 实验步骤

编写RegistDemo类,该类继承JFrame类。在窗体中增加文本域、密码域、按钮等组件实现注册功能。代码如下:

import javax.swing.*;

import javax.swing.border.EmptyBorder;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.lang.reflect.Array;

import java.util.Arrays;

public class RegistDemo extends JFrame {

    private static final long serialVersionUID = -7774133711807576073L;

    private JPanel contentPane;

    private JTextField textField;

    private JPasswordField passwordField1;

    private JPasswordField passwordField2;

    public static void main(String[] args) {

        try {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

        } catch (Throwable e) {

            e.printStackTrace();

        }

        EventQueue.invokeLater(new Runnable() {

            @Override

            public void run() {

                try {

                    RegistDemo frame = new RegistDemo();

                    frame.setVisible(true);

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

    }

    public RegistDemo() {

        setTitle("用户注册");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setBounds(100, 100, 300, 250);

        contentPane = new JPanel();

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        setContentPane(contentPane);

        contentPane.setLayout(new GridLayout(5, 1, 5, 5));

        JPanel panel1 = new JPanel();

        contentPane.add(panel1);

        JLabel label1 = new JLabel("新用户注册");

        panel1.add(label1);

        JPanel panel2 = new JPanel();

        FlowLayout flowLayout = (FlowLayout) panel2.getLayout();

        flowLayout.setAlignment(FlowLayout.LEFT);

        contentPane.add(panel2);

        JLabel label2 = new JLabel("用 户 名:");

        panel2.add(label2);

        textField = new JTextField();

        panel2.add(textField);

        textField.setColumns(18);

        JPanel panel3 = new JPanel();

        FlowLayout flowLayout1 = (FlowLayout) panel3.getLayout();

        flowLayout1.setAlignment(FlowLayout.LEFT);

        contentPane.add(panel3);

        JLabel label3 = new JLabel(" 密  码 :");

        panel3.add(label3);

        passwordField1 = new JPasswordField();

        passwordField1.setColumns(18);

        panel3.add(passwordField1);

        JPanel panel4 = new JPanel();

        FlowLayout flowLayout2 = (FlowLayout) panel4.getLayout();

        flowLayout2.setAlignment(FlowLayout.LEFT);

        contentPane.add(panel4);

        JLabel label4 = new JLabel("确认密码:");

        panel4.add(label4);

        passwordField2 = new JPasswordField();

        passwordField2.setColumns(18);

        panel4.add(passwordField2);

        JPanel panel5 = new JPanel();

        contentPane.add(panel5);

        JButton button = new JButton("提交");

        button.addActionListener(new AbstractAction() {

            @Override

            public void actionPerformed(ActionEvent e) {

                do_button_actionPerformed(e);

            }

        });

        panel5.add(button);

    }

    protected void do_button_actionPerformed(ActionEvent e) {

        char[] password1 = passwordField1.getPassword();

        char[] password2 = passwordField2.getPassword();

        boolean equals = Arrays.equals(password1, password2);

        if (equals) {

            JOptionPane.showMessageDialog(this, "用户注册成功!", null, JOptionPane.INFORMATION_MESSAGE);

        } else {

            JOptionPane.showMessageDialog(this, "两次密码不同!", null, JOptionPane.WARNING_MESSAGE);

        }

        Arrays.fill(password1, '0');

        Arrays.fill(password2, '0');

    }

}

  • 实验结果

运行程序,效果如图所示。

  1. 当两次输入密码不同时:

                                                       图 1

  1. 当两次输入密码相同时:

                                                       图 2

  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甜航一直在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值