0
点赞
收藏
分享

微信扫一扫

用户登录界面实现

小贴贴纸happy 2022-04-13 阅读 139
java

1.用户登录界面实现代码

代码如下(示例):

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

public class UserLogin {
    public static void main(String[] args) {
        //生成登录图形窗口

        //定义一个窗口
        JFrame jf = new JFrame();
        //定义窗口大小
        jf.setSize(400, 300);
        //窗口标题
        jf.setTitle("用户登录");
        //窗口关闭时程序停止
        jf.setDefaultCloseOperation(3);
        //取消原来的位置
        jf.setLayout(null);
        //使窗口位于中间
        jf.setLocationRelativeTo(null);
        //是窗口一直浮在上面
        jf.setAlwaysOnTop(true);

        //定义用户名
        JLabel jl1 = new JLabel("用户名");
        jl1.setBounds(50, 50, 50, 20);
        jf.add(jl1);

        //用户名输入框
        JTextField usernameField = new JTextField();
        usernameField.setBounds(150, 50, 180, 20);
        jf.add(usernameField);

        //定义密码
        JLabel jl2 = new JLabel("密码");
        jl2.setBounds(50, 100, 50, 20);
        jf.add(jl2);
        //密码输入框
        JTextField passwordField = new JTextField();
        passwordField.setBounds(150, 100, 180, 20);
        jf.add(passwordField);

        //定义一个登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(50, 200, 280, 20);
        jf.add(loginButton);

        //已知的用户名和密码
        String name = "helloworld";
        String pwd = "123456";

        //将输入的用户名和已知的用户名绑定
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //获取输入的用户名和密码名
                String username = usernameField.getText();
                String password = passwordField.getText();

                //判断输入的用户名和密码是否符合要求
                //用户名和密码的长度都是6-12位
                if (username.length() < 6 && username.length() > 12) {
//                    System.out.println("用户名输入长度为6-12位,请重新新输入");
                    //可以将输入结果直接在屏幕上体现
                    JOptionPane.showConfirmDialog(jf,"用户名输入长度为6-12位,请重新新输入");
                    usernameField.setText(" ");
                    return;
                }
                //用户名和密码的长度都是6-12位
         /*       if (password.length() < 6 && password.length() > 12) {
//                    System.out.println("密码输入长度为6-12位,请重新新输入");
                    静态成员和方法可以直接通过类名访问,不用生成对象
                    //可以将输入结果直接在屏幕上体现
                    JOptionPane.showConfirmDialog(jf,"密码输入长度为6-12位,请重新新输入");
                    passwordField.setText("");
                    return;
                }*/

                //在判断登录是否成功
                if(username.equals(name)&&password.equals(pwd)){
//                    System.out.println("登陆成功");
                    //使“登陆成功”在屏幕上显示
                    JOptionPane.showConfirmDialog(jf,"登陆成功");
                    //登陆成功后清空输入框;
                    usernameField.setText("");
                    passwordField.setText("");

                }else{
//                    System.out.println("用户名和密码输入长度为6-12位,请重新新输入");
                    使“错误提示”在屏幕上显示
                    JOptionPane.showConfirmDialog(jf,"用户名和密码输入长度为6-12位,请重新新输入");
                }
//
            }
        });

        //使窗口可见
        jf.setVisible(true);

    }
}

举报

相关推荐

0 条评论