Java教程

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();

            }

}



这篇关于javaGUI 登录窗口的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!