java swing 創(chuàng)建一個簡單的QQ界面教程
記錄自己用java swing做的第一個簡易界面。
LoginAction.java
package com.QQUI0819; import javax.swing.*; import java.awt.event.*; //首先,編寫按鈕癿監(jiān)聽器實現(xiàn)類 public class LoginAction implements ActionListener { private int count=0; //當前為null,創(chuàng)建后指向界面輸入框 private JTextField ulName; private JTextField ulpass; //創(chuàng)建時,輸入界面類中癿輸入框 public LoginAction(JTextField ulName,JTextField ulpass ){ this.ulName=ulName; this.ulpass=ulpass; } //實現(xiàn)接口中癿方法 //當勱作發(fā)生時,這個方法執(zhí)行 public void actionPerformed(ActionEvent e) { //點擊時,就取得界面輸入框癿內(nèi)容 //此時癿jtf,指向是界面上那個輸入框 String u=ulName.getText(); String p=ulName.getText(); System.out.println("賬號輸入的是 "+u); System.out.println("密碼輸入的是 "+p) if(u.equals("csy123") ||(p.equals("456"))){ //如果輸入正確,彈出新界面 JFrame jf=new JFrame(); jf.setTitle("登陸成功"); jf.setSize(300,400); jf.setLocationRelativeTo(null); jf.setVisible(true); } else { //如果輸入正確,彈出新界面 JFrame jf=new JFrame(); jf.setTitle("登陸失敗"); jf.setSize(300,100); JButton b1 = new JButton("登陸失敗,賬號和密碼不匹配"); jf.add(b1); jf.setLocationRelativeTo(null); jf.setVisible(true); } } }
QQ.java
package com.QQUI0819; import java.awt.Color; import java.awt.Container; import java.awt.Cursor; import java.awt.Font; import java.awt.Image; import java.util.ArrayList; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JRootPane; import javax.swing.JTextField; public class QQ extends JFrame{ //用戶名 private JTextField ulName; //密碼 private JPasswordField ulPasswd; //小容器 private JLabel j1; private JLabel j2; private JLabel j3; private JLabel j4; //小按鈕 private JButton b1; //復選框 private JCheckBox c1; private JCheckBox c2; /** * 初始化QQ登錄頁面 * */ public QQ(){ //設置登錄窗口標題 this.setTitle("QQ登錄"); //去掉窗口的裝飾(邊框) //采用指定的窗口裝飾風格 this.getRootPane().setWindowDecorationStyle(JRootPane.NONE); //窗體組件初始化 init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設置布局為絕對定位 this.setLayout(null); this.setBounds(0, 0, 355, 265); //設置窗體的圖標 Image img0 = new ImageIcon("E:\\藍杰培訓\\第一周java基礎\\basicStudy\\src\\com\\QQUI0819\\QQlogo.jpg").getImage(); this.setIconImage(img0); //窗體大小不能改變 this.setResizable(false); //居中顯示 this.setLocationRelativeTo(null); //窗體顯示 this.setVisible(true); } /** * 窗體組件初始化 * */ public void init(){ //創(chuàng)建一個容器,其中的圖片大小和setBounds Container container = this.getContentPane(); j1 = new JLabel(); //設置背景色 Image img1 = new ImageIcon("E:\\藍杰培訓\\第一周java基礎\\basicStudy\\src\\com\\QQUI0819\\QQ.jpg").getImage(); j1.setIcon(new ImageIcon(img1)); j1.setBounds(0, 0, 355, 90); //qq頭像設定 j2 = new JLabel(); Image img2 = new ImageIcon("E:\\藍杰培訓\\第一周java基礎\\basicStudy\\src\\com\\QQUI0819\\QQlogo.jpg").getImage(); j2.setIcon(new ImageIcon(img2)); j2.setBounds(20, 100, 60, 63); //用戶名輸入框 ulName = new JTextField(); ulName.setBounds(100, 100, 150, 20); //注冊賬號 j3 = new JLabel("注冊賬號"); j3.setBounds(260, 100, 70, 20); //密碼輸入框 ulPasswd = new JPasswordField(); ulPasswd.setBounds(100, 130, 150, 20); //找回密碼 j4= new JLabel("找回密碼"); j4.setBounds(260, 130, 70, 20); //記住密碼 c1 = new JCheckBox("記住密碼"); c1.setBounds(105, 155, 80, 15); //自動登陸 c2 = new JCheckBox("自動登陸"); c2.setBounds(185, 155, 80, 15); //登陸按鈕 b1 = new JButton("登錄"); //設置字體和顏色和手形指針 b1.setFont(new Font("宋體", Font.PLAIN, 12)); b1.setForeground(Color.black); b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); b1.setBounds(20, 200, 300, 20); //給按鈕添加 //所有組件用容器裝載 this.add(j1); this.add(j2); this.add(j3); this.add(j4); this.add(c1); this.add(c2); this.add(b1); //創(chuàng)建監(jiān)聽器對象,幵加給按鈕 LoginAction lo=new LoginAction(ulName,ulPasswd); b1.addActionListener(lo); container.add(j1); container.add(ulName); container.add(ulPasswd); } public static void main(String[] args) { new QQ(); } }
效果圖
素材
logo:
QQ logo:
補充知識:創(chuàng)建一個簡單的Java Swing登入窗口
連接數(shù)據(jù)圖就不弄了,要連接數(shù)據(jù)庫的自行添加
為了方便排版,Layout用SpringLayout類型
1.賬號框采用JTextField 類型
2.密碼框采用JPasswordField類型
兩者的區(qū)別是:
JPasswordField顯示的文本是這樣的:
文本框JTextField顯示的文本是這樣的:
3.建立兩個JButton,一個是“登入”按鈕,另一個是“重置”按鈕
4.建立兩個Checkbox:一個是記住密碼,另一個是隱身登入
5.最后一步加入背景圖
代碼:
package l; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.SpringLayout; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JPasswordField; import javax.swing.ImageIcon; import javax.swing.JButton; import java.awt.event.FocusAdapter; import java.awt.event.FocusEvent; import javax.swing.JLayeredPane; import java.awt.Checkbox; public class Login extends JFrame { private JPanel contentPane; private JTextField textField; private JPasswordField passwordField; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Login frame = new Login(); frame.setVisible(true); //窗口可見 frame.setResizable(false); //設置登入窗口不能調(diào)整大小 ImageIcon img = new ImageIcon("d:/img.jpg");//這是背景圖片 JLabel imgLabel = new JLabel(img);//將背景圖放在標簽里。 frame.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE)); imgLabel.setBounds(0,0,img.getIconWidth(), img.getIconHeight()); Container cp=frame.getContentPane(); ((JPanel)cp).setOpaque(false); } catch (Exception e) { e.printStackTrace(); } } }); } public Login() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); SpringLayout sl_contentPane = new SpringLayout(); contentPane.setLayout(sl_contentPane); JLabel lblNewLabel = new JLabel("賬號:"); lblNewLabel.setFont(new Font("黑體", Font.PLAIN, 19)); sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel, 34, SpringLayout.NORTH, contentPane); sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel, 41, SpringLayout.WEST, contentPane); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("密碼:"); lblNewLabel_1.setFont(new Font("黑體", Font.PLAIN, 19)); sl_contentPane.putConstraint(SpringLayout.NORTH, lblNewLabel_1, 29, SpringLayout.SOUTH, lblNewLabel); sl_contentPane.putConstraint(SpringLayout.WEST, lblNewLabel_1, 0, SpringLayout.WEST, lblNewLabel); contentPane.add(lblNewLabel_1); textField = new JTextField(); textField.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { String temp=textField.getText(); if(temp.equals("")) { textField.setForeground(Color.GRAY); //JTextField提示輸入QQ號 textField.setText("請輸入QQ號"); } } public void focusGained(FocusEvent e) { String temp=textField.getText(); if(temp.equals("請輸入QQ號")) { textField.setText(""); } } }); sl_contentPane.putConstraint(SpringLayout.NORTH, textField, 1, SpringLayout.NORTH, lblNewLabel); sl_contentPane.putConstraint(SpringLayout.WEST, textField, 52, SpringLayout.EAST, lblNewLabel); sl_contentPane.putConstraint(SpringLayout.EAST, textField, 196, SpringLayout.EAST, lblNewLabel); contentPane.add(textField); textField.setColumns(10); passwordField = new JPasswordField(); sl_contentPane.putConstraint(SpringLayout.NORTH, passwordField, 26, SpringLayout.SOUTH, textField); sl_contentPane.putConstraint(SpringLayout.WEST, passwordField, 52, SpringLayout.EAST, lblNewLabel_1); sl_contentPane.putConstraint(SpringLayout.SOUTH, passwordField, 48, SpringLayout.SOUTH, textField); sl_contentPane.putConstraint(SpringLayout.EAST, passwordField, 201, SpringLayout.EAST, lblNewLabel_1); contentPane.add(passwordField); JButton btnNewButton = new JButton("登入"); sl_contentPane.putConstraint(SpringLayout.SOUTH, btnNewButton, 65, SpringLayout.SOUTH, passwordField); contentPane.add(btnNewButton); JButton button = new JButton("重置"); sl_contentPane.putConstraint(SpringLayout.NORTH, btnNewButton, 2, SpringLayout.NORTH, button); sl_contentPane.putConstraint(SpringLayout.EAST, btnNewButton, -70, SpringLayout.WEST, button); sl_contentPane.putConstraint(SpringLayout.NORTH, button, 32, SpringLayout.SOUTH, passwordField); sl_contentPane.putConstraint(SpringLayout.WEST, button, 242, SpringLayout.WEST, contentPane); button.setFont(new Font("宋體", Font.PLAIN, 19)); contentPane.add(button); Checkbox checkbox = new Checkbox("記住密碼"); sl_contentPane.putConstraint(SpringLayout.WEST, checkbox, 0, SpringLayout.WEST, textField); sl_contentPane.putConstraint(SpringLayout.SOUTH, checkbox, -6, SpringLayout.NORTH, btnNewButton); contentPane.add(checkbox); Checkbox checkbox_1 = new Checkbox("隱身登入"); sl_contentPane.putConstraint(SpringLayout.WEST, checkbox_1, 35, SpringLayout.EAST, checkbox); sl_contentPane.putConstraint(SpringLayout.SOUTH, checkbox_1, 0, SpringLayout.SOUTH, checkbox); contentPane.add(checkbox_1); } }
運行截圖:
以上這篇java swing 創(chuàng)建一個簡單的QQ界面教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Java Swing窗體關閉事件的調(diào)用關系
- Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法示例
- Java Swing實現(xiàn)JTable檢測單元格數(shù)據(jù)變更事件的方法示例
- JavaSwing基礎之Layout布局相關知識詳解
- Java Swing實現(xiàn)坦克大戰(zhàn)游戲
- Java Swing最詳細基礎知識總結
- Java實戰(zhàn)之基于swing的QQ郵件收發(fā)功能實現(xiàn)
- Eclipse+Java+Swing實現(xiàn)學生成績管理系統(tǒng)的實例代碼
- Java Swing 只關閉當前窗體的實現(xiàn)
- Java基礎學習之Swing事件監(jiān)聽
相關文章
AQS(AbstractQueuedSynchronizer)抽象隊列同步器及工作原理解析
AQS是用來構建鎖或者其他同步器組件的重量級基礎框架及整個JUC體系的基石,通過內(nèi)置的FIFO對列來完成資源獲取線程的排隊工作,并通過一個int類型變量表示持有鎖的狀態(tài),本文給大家詳細介紹下AQS抽象隊列同步器的相關知識,感興趣的朋友一起看看吧2022-03-03SpringMVC中的HandlerMapping和HandlerAdapter詳解
這篇文章主要介紹了SpringMVC中的HandlerMapping和HandlerAdapter詳解,在Spring MVC中,HandlerMapping(處理器映射器)用于確定請求處理器對象,請求處理器可以是任何對象,只要它們使用了@Controller注解或注解@RequestMapping,需要的朋友可以參考下2023-08-08詳解JAVA中的Collection接口和其主要實現(xiàn)的類
這篇文章主要介紹了JAVA中的Collection接口和其主要實現(xiàn)的類,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03Java中for、foreach、stream區(qū)別和性能比較詳解
for、foreach、stream都可以循環(huán)處理數(shù)據(jù),如果單純當循環(huán)使用,for、foreach、stream哪個性能更好,這篇文章主要給大家介紹了關于Java中for、foreach、stream區(qū)別和性能的相關資料,需要的朋友可以參考下2024-03-03SpringBoot3?Web編程開發(fā)的工程搭建攔截器及測試工具示例
這篇文章主要介紹了SpringBoot3?Web編程開發(fā)的工程搭建攔截器及測試工具示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08