zoukankan      html  css  js  c++  java
  • 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

     

  • 相关阅读:
    吴恩达深度学习笔记 course2 week1 作业1
    吴恩达深度学习笔记 course2 week1 测验
    吴恩达深度学习笔记 course 2 1.1~1.14 深度学习的实用层面
    吴恩达深度学习笔记第四章作业2
    开发中经常遇到SVN清理失败的问题:
    UE4编程之C++创建一个FPS工程(一)创建模式&角色&处理输入
    Unreal Engine 4 C++ UCLASS构造函数易出错分析
    光照构建失败。Swarm启动失败
    ue4 C++ 生成并弹出一个菜单
    解决SVN Cleanup错误: Failed to run the WC DB work queue associated with
  • 原文地址:https://www.cnblogs.com/zhanzhuang/p/9993673.html
Copyright © 2011-2022 走看看