java 2 swing_Java Swing2 基本登錄框

用到了標簽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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值