一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java swing 創(chuàng)建一個簡單的QQ界面教程

java swing 創(chuàng)建一個簡單的QQ界面教程

2020-09-12 14:57陳思源一點也不圓 Java教程

這篇文章主要介紹了java swing 創(chuàng)建一個簡單的QQ界面教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

記錄自己用java swing做的第一個簡易界面。

LoginAction.java

?
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
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

?
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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(){
 //設(shè)置登錄窗口標題
 this.setTitle("QQ登錄");
 //去掉窗口的裝飾(邊框)
 //采用指定的窗口裝飾風格
 this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
 //窗體組件初始化
 init();
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 //設(shè)置布局為絕對定位
 this.setLayout(null);
 this.setBounds(0, 0, 355, 265);
 //設(shè)置窗體的圖標
 Image img0 = new ImageIcon("E:\\藍杰培訓\\第一周java基礎(chǔ)\\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();
 //設(shè)置背景色
 Image img1 = new ImageIcon("E:\\藍杰培訓\\第一周java基礎(chǔ)\\basicStudy\\src\\com\\QQUI0819\\QQ.jpg").getImage();
 j1.setIcon(new ImageIcon(img1));
 j1.setBounds(0, 0, 355, 90);
 //qq頭像設(shè)定
 j2 = new JLabel();
 Image img2 = new ImageIcon("E:\\藍杰培訓\\第一周java基礎(chǔ)\\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("登錄");
 //設(shè)置字體和顏色和手形指針
 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();
 }
}

效果圖

java swing 創(chuàng)建一個簡單的QQ界面教程

java swing 創(chuàng)建一個簡單的QQ界面教程

素材

logo:

java swing 創(chuàng)建一個簡單的QQ界面教程

QQ logo:

java swing 創(chuàng)建一個簡單的QQ界面教程

補充知識:創(chuàng)建一個簡單的Java Swing登入窗口

連接數(shù)據(jù)圖就不弄了,要連接數(shù)據(jù)庫的自行添加

為了方便排版,Layout用SpringLayout類型

1.賬號框采用JTextField 類型

2.密碼框采用JPasswordField類型

兩者的區(qū)別是:

JPasswordField顯示的文本是這樣的:

java swing 創(chuàng)建一個簡單的QQ界面教程

文本框JTextField顯示的文本是這樣的:

java swing 創(chuàng)建一個簡單的QQ界面教程

3.建立兩個JButton,一個是“登入”按鈕,另一個是“重置”按鈕

4.建立兩個Checkbox:一個是記住密碼,另一個是隱身登入

5.最后一步加入背景圖

 

代碼:

?
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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);           //設(shè)置登入窗口不能調(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界面教程

以上這篇java swing 創(chuàng)建一個簡單的QQ界面教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/weixin_49857492/article/details/108513573

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产欧美一区二区成人影院 | 久久精品视在线观看85 | 欧美作爱福利免费观看视频 | 亚洲欧美精品久久 | 成人精品一级毛片 | 肉车各种play文r | 热99re久久精品国产 | 99国内精品久久久久久久黑人 | 国产美女久久精品香蕉69 | 国产欧美视频在线观看 | 精品无人区乱码1区2区3区在线 | 亚洲AV无码国产精品午夜久久 | 500福利第一导航 | 亚洲人成网站在线观看妞妞网 | 美女被到爽流动漫 | 青青草原手机在线视频 | 99热这里只有精品国产免费 | 2022色婷婷综合久久久 | 日本理论片中文在线观看2828 | 青青青在线观看国产精品 | 欧美午夜寂寞影院安卓列表 | 国产午夜视频在线观看网站 | 亚洲 欧美 清纯 校园 另类 | 国内精品一区二区在线观看 | 欧美特级特黄a大片免费 | 国产色综合久久五月色婷婷中文 | 天堂欧美 | 大肚孕妇的高h辣文 | 2022日韩理论片在线观看 | 亚洲欧美久久久久久久久久爽网站 | 日韩欧美一区二区不卡 | 免费看男人狂躁女人 | 亚洲国产综合自在线另类 | 精品无码人妻一区二区免费AV | 国产人va在线 | 日韩在线一区二区三区 | xnxx老师 | 成人综合婷婷国产精品久久免费 | 1377大但人文艺术包子铺 | 日本一区二区三区久久 | 亚洲乱人伦在线 |