本文主要分享了關于簡潔版qq登錄界面及按鈕顏色設置的相關代碼,供參考。
java代碼塊
公共包(初始化窗口位置)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package util; import java.awt.dimension; import java.awt.toolkit; import javax.swing.jframe; //圖形化界面的工具類 public class frameutil { //設置窗體出現在中間位置 public static void initframe(jframe frame, int width, int height ) { //獲取默認系統工具包 toolkit toolkit = toolkit.getdefaulttoolkit(); //獲取屏幕的分辨率 dimension dimension = toolkit.getscreensize(); int x = ( int )dimension.getwidth(); int y = ( int )dimension.getheight(); frame.setbounds((x-width)/ 2 , (y-height)/ 2 , width, height); //設置窗體的可見性 frame.setvisible( true ); //設置窗體關閉 frame.setdefaultcloseoperation(jframe.exit_on_close); } } |
簡易qq登錄界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
public static void main(string[] args) { // todo auto-generated method stub //創建新框架對象 jframe frame = new jframe( "qq登錄程序" ); //調用框架初始化方法 frameutil.initframe(frame, 500 , 350 ); //創建新的面 jpanel panel = new jpanel(); frame.add(panel); //不使用布局管理 panel.setlayout( null ); //qq號的標簽 jlabel namelable = new jlabel( "qq號:" ); jtextfield namefiled = new jtextfield(); panel.add(namelable); panel.add(namefiled); namelable.setbounds( 130 , 130 , 300 , 25 ); namefiled.setbounds( 175 , 130 , 150 , 25 ); //密碼標簽 jlabel passlable = new jlabel( "密 碼:" ); jpasswordfield passwordfield = new jpasswordfield(); panel.add(passlable); panel.add(passwordfield); passlable.setbounds( 130 , 160 , 300 , 25 ); passwordfield.setbounds( 175 , 160 , 150 , 25 ); //記住密碼復選項 jcheckbox rememberpassword = new jcheckbox( "記住密碼" ); panel.add(rememberpassword); rememberpassword.setbounds( 170 , 190 , 80 , 14 ); //自動登錄復選項 jcheckbox autologin = new jcheckbox( "自動登錄" ); panel.add(autologin); autologin.setbounds( 250 , 190 , 80 , 14 ); //登錄按鈕 jbutton login = new jbutton( "登 錄" ); panel.add(login); login.setbounds( 175 , 220 , 150 , 25 ); //注冊賬號按鈕 jbutton newnumber = new jbutton( "注冊賬號" ); panel.add(newnumber); newnumber.setbounds( 335 , 130 , 90 , 25 ); //找回密碼按鈕 jbutton findpassword = new jbutton( "找回密碼" ); panel.add(findpassword); findpassword.setbounds( 335 , 160 , 90 , 25 ); } |
運行結果
按鈕及其添加顏色
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package swing; import util.*; import java.awt.color; import java.awt.gridlayout; import javax.swing.jbutton; import javax.swing.jframe; public class buttons { public static void main(string[] args) { // todo auto-generated method stub jframe frame = new jframe( "buttons" ); //使用表格管理者,一行十列 gridlayout gridlayout = new gridlayout( 1 , 10 ); frame.setlayout(gridlayout); //創建按鈕數組儲存按鈕 jbutton[] buttons = new jbutton[ 10 ]; //創建十個按鈕賦予數字文本 for ( int i= 0 ;i< 10 ;i++) { buttons[i] = new jbutton(integer.tostring(i)); frame.add(buttons[i]); } //按鈕上色 buttons[ 0 ].setbackground(color.yellow); buttons[ 1 ].setbackground(color.cyan); buttons[ 2 ].setbackground(color.blue); buttons[ 3 ].setbackground(color.dark_gray); buttons[ 4 ].setbackground(color.gray); buttons[ 5 ].setbackground(color.green); buttons[ 6 ].setbackground(color.magenta); buttons[ 7 ].setbackground(color.orange); buttons[ 8 ].setbackground(color.red); buttons[ 9 ].setbackground(color.pink); //后顯示框架防止運行不顯示而需要拖動界面 frameutil.initframe(frame, 800 , 600 ); } } |
運行結果
其他功能模塊大家可自行補充。
總結
以上就是本文關于java代碼塊之簡易qq登錄界面及按鈕顏色設置代碼的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。
原文鏈接:https://www.2cto.com/kf/201711/698868.html