• java swing 制作一个登陆界面,亲测有效


    一、介绍

    Swing 是一个为Java设计的GUI工具包。

    Swing是JAVA基础类的一部分。

    Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

    Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

    二、效果

    三、代码

    package com.test.jframe;
    
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFormattedTextField;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPasswordField;
    
    public class JFrameTest {
    
        private JFrame frame;
        private JPasswordField passwordField;
        private boolean isLogin = false;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        JFrameTest window = new JFrameTest();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public JFrameTest() {
            initialize();
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
    
            String userName = "111";
            String userPwd = "111";
    
            frame = new JFrame();
            frame.setBounds(100, 100, 667, 453);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
    
            Label label = new Label("账号:");
            label.setAlignment(Label.CENTER);
            label.setBounds(116, 49, 50, 23);
            frame.getContentPane().add(label);
    
            Label label_1 = new Label("密码:");
            label_1.setAlignment(Label.CENTER);
            label_1.setBounds(116, 85, 50, 23);
            frame.getContentPane().add(label_1);
    
            Label label_2 = new Label("用户状态:");
            label_2.setBounds(433, 49, 60, 23);
            frame.getContentPane().add(label_2);
    
            Label label_3 = new Label("未登录");
            label_3.setForeground(new Color(255, 0, 0));
            label_3.setBounds(499, 49, 40, 23);
            frame.getContentPane().add(label_3);
    
            JFormattedTextField formattedTextField = new JFormattedTextField();
            formattedTextField.setBounds(172, 49, 166, 23);
            frame.getContentPane().add(formattedTextField);
    
            passwordField = new JPasswordField();
            passwordField.setBounds(172, 85, 166, 23);
            frame.getContentPane().add(passwordField);
    
            JButton button = new JButton("login");
            button.setBackground(new Color(255, 255, 255));
            button.setBounds(126, 121, 212, 23);
            frame.getContentPane().add(button);
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String getUserName = formattedTextField.getText();
                    String getUserPwd = passwordField.getText();
                    if (userName.equals(getUserName) && userPwd.equals(getUserPwd)) {
                        isLogin = true;
                    } else {
                        isLogin = false;
                    }
                    if (isLogin) {
                        JOptionPane.showMessageDialog(null, "登录成功!", "消息", JOptionPane.PLAIN_MESSAGE);
                        label_3.setText("已登录");
                        label_3.setForeground(Color.BLUE);
                    } else {
                        JOptionPane.showMessageDialog(null, "登录失败!", "消息", JOptionPane.WARNING_MESSAGE);
                        label_3.setText("未登录");
                        label_3.setForeground(Color.RED);
                    }
                }
            });
        }
    }

     四、解决中文乱码问题

    Run as > Run Condiguration,在Arguments中增加下面这句话:

    -Dfile.encoding=gbk

     

  • 相关阅读:
    nodejs启动前端项目步骤
    多线程
    数组排序(6)- 快速排序
    数组排序(5)- 归并排序
    数组排序(4)- 希尔排序
    数组排序(3)-插入排序
    数组排序(2)-选择排序
    数组排序(1)-冒泡排序
    队列-用非顺序映像实现队列
    ArrayList、Vector和LinkedList的区别
  • 原文地址:https://www.cnblogs.com/zhanzhuang/p/9993673.html
Copyright © 2020-2023  润新知