用到了標簽JLabel
輸入域 JTextField
密碼輸入域JPasswordField
按鈕 JButton
package com.igswing.zmz;
import java.awt.Frame;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Swingtwo {
public static void main(String[] args) {
JFrame myFrame = new JFrame("Login Exanple");
myFrame.setSize(350, 200);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
/* 創建面板,這個類似於 HTML 的 div 標簽
* 我們可以創建多個面板並在 JFrame 中指定位置
* 面板中我們可以添加文本字段,按鈕及其他組件。
*/
JPanel panel = new JPanel();
//添加面板
myFrame.add(panel);
/*
調用用戶定義方法並添加組件到面板
*/
placeComponents(panel);
myFrame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
// TODO Auto-generated method stub
/**
* 為panel的布局的方法
*/
panel.setLayout(null);
JLabel userLabel = new JLabel("User: ");
/* 這個方法定義了組件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
/*
* 創建文本域用於用戶輸入
*/
JTextField userText = new JTextField(20);//參數列
userText.setBounds(100,20,165,25);//縱向寬度是和上面對應的
panel.add(userText);
/**
* 輸入密碼的文本域
*/
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
/**
* 這個類似用於輸入的文本域
* 但是輸入的信息會以點號代替,用於包含密碼的安全性
*/
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(100,50,165,25);//輸入框的長度和上面也相等
panel.add(passwordText);
//創建登錄按鈕
JButton loginButton = new JButton("login");
loginButton.setBounds(10,80,80,25);
panel.add(loginButton);
}
}