编程语言
首页 > 编程语言> > javaGUI 登录窗口

javaGUI 登录窗口

作者:互联网

创建一个登录窗口

在这里插入图片描述

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

public class loginFrame extends JFrame {

    public loginFrame(){
         this.setSize(300,200);
//         this.setLocation(100,100);
        this.setLocationRelativeTo(null);
        this.setTitle("登录QQ");
        this.setIconImage(new ImageIcon("屏幕截图 2021-12-11 170051.png").getImage());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setLayout(new GridLayout(3,1));

        JPanel jp = new JPanel();
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();

        JButton jb = new JButton("确定");
                jb.setLocation(100,50);
                jb.setSize(100,40);

        JButton jb1 = new JButton("取消");
                jb1.setLocation(100,100);
                jb1.setSize(100,40);

        JLabel accountLabel = new JLabel("账号",JLabel.CENTER);

        JTextField accountText = new JTextField(10);



        JLabel passWord = new JLabel("密码");

        JPasswordField passwordField = new JPasswordField(10);

        jp.add(accountLabel);
        jp.add(accountText);
        jp1.add(passWord);
        jp1.add(passwordField);
        jp2.add(jb);
        jp2.add(jb1);

        this.add(jp);
        this.add(jp1);
        this.add(jp2);

        this.setVisible(true);

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String account = accountText.getText();


                    String password = new String(passwordField.getPassword());

                    if (account.length() == 0) {
                        JOptionPane.showMessageDialog(null, "账号不能为空", "系统提示", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    if (password.length() == 0) {
                        JOptionPane.showMessageDialog(null, "密码不能为空");
                        return;
                    }
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "运行异常");
                }
            }
        });



        jb1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    accountText.setText("");
                    passwordField.setText("");

                }
        });
    }



            public static void main(String[] args) {

                new loginFrame();

            }

}



标签:窗口,登录,JPanel,javaGUI,public,add,new,100,JLabel
来源: https://blog.csdn.net/weixin_56781779/article/details/121916833