java swing 製做一個登錄界面,親測有效

1、介紹java

Swing 是一個爲Java設計的GUI工具包。app

Swing是JAVA基礎類的一部分。工具

Swing包括了圖形用戶界面(GUI)器件如:文本框,按鈕,分隔窗格和表。spa

Swing提供許多比AWT更好的屏幕顯示元素。它們用純Java寫成,因此同Java自己同樣能夠跨平臺運行,這一點不像AWT。它們是JFC的一部分。它們支持可更換的面板和主題(各類操做系統默認的特有主題),然而不是真的使用原平生臺提供的設備,而是僅僅在表面上模仿它們。這意味着你能夠在任意平臺上使用JAVA支持的任意麪板。輕量級組件的缺點則是執行速度較慢,優勢就是能夠在全部平臺上採用統一的行爲。操作系統

2、效果設計

3、代碼3d

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

 4、解決中文亂碼問題code

Run as > Run Condiguration,在Arguments中增長下面這句話:orm

-Dfile.encoding=gbkblog

 

相關文章
相關標籤/搜索