本文主要分享了关于简洁版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