java代碼塊之簡易qq登錄界面及按鈕顏色設置代碼
本文主要分享了關于簡潔版qq登錄界面及按鈕顏色設置的相關代碼,供參考。
java代碼塊
公共包(初始化窗口位置)
package util;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
//圖形化界面的工具類
public class FrameUtil {
//設置窗體出現(xiàn)在中間位置
public static void initFrame(JFrame frame,int width,int height ) {
//獲取默認系統(tǒng)工具包
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
//創(chuàng)建新框架對象
JFrame frame = new JFrame("QQ登錄程序");
//調用框架初始化方法
FrameUtil.initFrame(frame, 500, 350);
//創(chuàng)建新的面
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);
//創(chuàng)建按鈕數(shù)組儲存按鈕
JButton[] buttons = new JButton[10];
//創(chuàng)建十個按鈕賦予數(shù)字文本
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登錄界面及按鈕顏色設置代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
java實現(xiàn)隊列數(shù)據(jù)結構代碼詳解
如有不足之處,歡迎留言指出。
相關文章
SpringBoot DevTools之開發(fā)工具與熱部署機制詳解
本文將深入探討Spring Boot DevTools的核心功能、配置方法以及最佳實踐,幫助開發(fā)者顯著提升開發(fā)效率,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
Java統(tǒng)計50個10到50之間整數(shù)的隨機出現(xiàn)次數(shù)
這篇文章主要為大家詳細介紹了Java統(tǒng)計50個10到50之間整數(shù)的隨機出現(xiàn)次數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Spring?Cloud?Gateway中netty線程池優(yōu)化示例詳解
這篇文章主要介紹了Spring?Cloud?Gateway中netty線程池優(yōu)化示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
Spring Jpa多數(shù)據(jù)源工程配置過程解析
這篇文章主要介紹了Spring Jpa多數(shù)據(jù)源工程配置過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
Netty分布式NioEventLoop任務隊列執(zhí)行源碼分析
這篇文章主要為大家介紹了Netty分布式NioEventLoop任務隊列執(zhí)行源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-03-03

