1. 程式人生 > >java swing 製作一個登陸介面,親測有效

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