首页 > 编程 > Java > 正文

java代码块之简易qq登录界面及按钮颜色设置代码

2019-11-26 10:54:12
字体:
来源:转载
供稿:网友

本文主要分享了关于简洁版qq登录界面及按钮颜色设置的相关代码,供参考。

java代码块

公共包(初始化窗口位置)

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登录界面

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

运行结果

按钮及其添加颜色

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登录界面及按钮颜色设置代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

java之StringBuffer常见使用方法解析

Java多线程之显示锁和内置锁总结详解

java实现队列数据结构代码详解

如有不足之处,欢迎留言指出。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表