swing实现简单的用户登录界面

界面由七个组件组成,两个标签,两个文本输入框,两个按钮,一个标签用来显示通知。

两个标签是用来显示“用户名”和“密码”;
两个文本输入框用来实现账户和密码的输入;
两个按钮分别具有登录和重置功能;
最后的标签用来通知登陆成功与否。

这串代码用来比较账户和密码是否匹配,主要目的是用来学习swing,所以这个很浅


```java
class LoginCheck{
    private String name;
    private String password;
    public LoginCheck(String name,String password){
        this.name = name;
        this.password = password;
    }
    public boolean validate(){
        if ("java".equals(name) && "yyds".equals(password)){
            return true;
        }else{
            return false;
        }
    }
}

下面这串代码是界面的绘制代码

class User_Login{
    private JFrame frame = new JFrame("欢迎您");
    private JButton submit = new JButton("登录");
    private JButton reset = new JButton("重置");
    private JLabel nameLab = new JLabel("用户名:");
    private JLabel passLab = new JLabel("密  码:");
    private JLabel infoLab = new JLabel("用户登录系统");
    private JTextField nameText = new JTextField();
    private JPasswordField passText = new JPasswordField();
    public User_Login(){
        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == submit){
                    String name = nameText.getText();
                    String password = new String(passText.getPassword());
                    LoginCheck login = new LoginCheck(name,password);
                    if(login.validate()){
                        infoLab.setText("登录成功,欢迎光临");
                    }else{
                        infoLab.setText("账号或密码错误");
                    }
                }
            }
        });
        reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == reset){
                    nameText.setText(null);
                    passText.setText(null);
                    infoLab.setText("用户登录系统");
                }
            }
        });
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("成功关闭");
                System.exit(1);
            }
        });
        frame.setLayout(null);
        nameLab.setBounds(5,5,60,20);
        passLab.setBounds(5,30,60,20);
        infoLab.setBounds(5,65,220,30);
        nameText.setBounds(65,5,100,20);
        passText.setBounds(65,30,100,20);
        submit.setBounds(165,5,60,20);
        reset.setBounds(165,30,60,20);
        frame.add(nameLab);
        frame.add(passLab);
        frame.add(infoLab);
        frame.add(nameText);
        frame.add(passText);
        frame.add(submit);
        frame.add(reset);
        frame.setSize(280,130);
        frame.setLocation(400,300);
        frame.setVisible(true);
    }
}
class MyActionEventDemo1{
    public static void main(String[] args) {
        new User_Login();
    }
}

所有代码仅仅只能实现登录界面的绘制和通知的显示,这就是全部功能(自己为了学习理解swing敲的)

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单Java Swing实现用户登录界面的示例代码: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Login extends JFrame implements ActionListener { JLabel userLabel, passwordLabel; JTextField userText; JPasswordField passwordText; JButton loginButton, cancelButton; public Login() { setTitle("用户登录"); setSize(300, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); userLabel = new JLabel("用户名:"); panel.add(userLabel); userText = new JTextField(20); panel.add(userText); passwordLabel = new JLabel("密码:"); panel.add(passwordLabel); passwordText = new JPasswordField(20); panel.add(passwordText); loginButton = new JButton("登录"); loginButton.addActionListener(this); panel.add(loginButton); cancelButton = new JButton("取消"); cancelButton.addActionListener(this); panel.add(cancelButton); add(panel); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == loginButton) { String user = userText.getText(); String password = new String(passwordText.getPassword()); // 进行用户登录验证,此处省略具体实现 JOptionPane.showMessageDialog(this, "登录成功!"); } else if (e.getSource() == cancelButton) { System.exit(0); } } public static void main(String[] args) { new Login(); } } ``` 该示例代码使用了Java Swing实现一个简单用户登录界面,包含了用户名、密码输入框、登录按钮和取消按钮。登录按钮与取消按钮均添加了事件监听器,当用户点击登录按钮时会进行用户登录验证,验证成功后会弹出一个提示框显示“登录成功”,当用户点击取消按钮时会退出程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值