前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >用户登录界面[通俗易懂]

用户登录界面[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-20 19:51:31
发布2022-09-20 19:51:31
3.3K00
代码可运行
举报
运行总次数:0
代码可运行

大家好,又见面了,我是你们的朋友全栈君。

代码语言:javascript
代码运行次数:0
运行
复制
public class LoginIn {
    private String name;
    private String password;
    public LoginIn(String name,String password){
        this.name = name;
        this.password = password;
    }

    public boolean checkOut() {
        if (("1" .equals(name)) && ("123456".equals(password))){
            return true;
        } else {
            return false;
        }
    }
}
代码语言:javascript
代码运行次数:0
运行
复制
package Practice.demo21AWT; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class ActionHandle { private JFrame frame = new JFrame("用户登录系统"); private JButton login = new JButton("登录"); private JButton reset = new JButton("重置"); private JLabel lab = new JLabel("用户登录系统"); private JLabel nameLab = new JLabel("用户名:"); private JLabel passwordLab = new JLabel("密 码:"); private JTextField nameText = new JTextField(); private JPasswordField passwordText = new JPasswordField(); public ActionHandle() { nameLab.setBounds(5,5,60,20); passwordLab.setBounds(5,40,60,20); nameText.setBounds(65,5,220,30); passwordText.setBounds(65,40,220,30); login.setBounds(70,100,60,20); reset.setBounds(170,100,60,20); lab.setBounds(75,150,50,40); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.out.println("窗口关闭"); } }); login.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == login){ String name = nameText.getText(); String password = new String(passwordText.getPassword()); LoginIn log = new LoginIn(name, password); if (log.checkOut() == true){ lab.setText("登陆成功!"); } else { lab.setText("登录失败!请检查用户名或密码"); } } } }); reset.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == reset){ nameText.setText(""); passwordText.setText(""); lab.setText("用户登录系统"); } } }); frame.add(nameLab); frame.add(passwordLab); frame.add(nameText); frame.add(passwordText); frame.add(login); frame.add(reset); frame.add(lab); frame.setSize(350,200); frame.setLocation(500,300); frame.setVisible(true); } }
代码语言:javascript
代码运行次数:0
运行
复制
public class MyActionEventDemo01 { public static void main(String[] args) { new ActionHandle(); } }

测试结果如下:

输入错误用户名或密码时

点击重置按钮会返回开始时界面

输入正确的用户名和密码时,显示登录成功

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/166531.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档