1. 程式人生 > 程式設計 >java swing 建立一個簡單的QQ介面教程

java swing 建立一個簡單的QQ介面教程

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

LoginAction.java

package com.QQUI0819;

import javax.swing.*;
import java.awt.event.*;
//首先,編寫按鈕癿監聽器實現類
public class LoginAction implements
  ActionListener {
 private int count=0;
 //當前為null,建立後指向介面輸入框
 private JTextField ulName;
 private JTextField ulpass;
 //建立時,輸入介面類中癿輸入框
 public LoginAction(JTextField ulName,JTextField ulpass ){
  this.ulName=ulName;
  this.ulpass=ulpass;
 }

 //實現介面中癿方法
 //當勱作發生時,這個方法執行
 public void actionPerformed(ActionEvent e) {
  //點選時,就取得介面輸入框癿內容
  //此時癿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,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(){
 //建立一個容器,其中的圖片大小和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,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,150,20);
 //註冊賬號
 j3 = new JLabel("註冊賬號");
 j3.setBounds(260,70,20);
 //密碼輸入框
 ulPasswd = new JPasswordField();
 ulPasswd.setBounds(100,130,20);
 //找回密碼
 j4= new JLabel("找回密碼");
 j4.setBounds(260,20);
 //記住密碼
 c1 = new JCheckBox("記住密碼");
 c1.setBounds(105,155,80,15);
 //自動登陸
 c2 = new JCheckBox("自動登陸");
 c2.setBounds(185,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);
 //建立監聽器物件,幵加給按鈕
 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 建立一個簡單的QQ介面教程

java swing 建立一個簡單的QQ介面教程

素材

logo:

java swing 建立一個簡單的QQ介面教程

QQ logo:

java swing 建立一個簡單的QQ介面教程

補充知識:建立一個簡單的Java Swing登入視窗

連線資料圖就不弄了,要連線資料庫的自行新增

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

1.賬號框採用JTextField 型別

2.密碼框採用JPasswordField型別

兩者的區別是:

JPasswordField顯示的文字是這樣的:

java swing 建立一個簡單的QQ介面教程

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

java swing 建立一個簡單的QQ介面教程

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);           //設定登入視窗不能調整大小
          ImageIcon img = new ImageIcon("d:/img.jpg");//這是背景圖片
           JLabel imgLabel = new JLabel(img);//將背景圖放在標籤裡。
           frame.getLayeredPane().add(imgLabel,new Integer(Integer.MIN_VALUE));     
           imgLabel.setBounds(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,450,300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5,5,5));
    setContentPane(contentPane);
    SpringLayout sl_contentPane = new SpringLayout();
    contentPane.setLayout(sl_contentPane);
    
    JLabel lblNewLabel = new JLabel("賬號:");
    lblNewLabel.setFont(new Font("黑體",19));
    sl_contentPane.putConstraint(SpringLayout.NORTH,lblNewLabel,34,SpringLayout.NORTH,contentPane);
    sl_contentPane.putConstraint(SpringLayout.WEST,41,SpringLayout.WEST,contentPane);
    contentPane.add(lblNewLabel);
    
    JLabel lblNewLabel_1 = new JLabel("密碼:");
    lblNewLabel_1.setFont(new Font("黑體",lblNewLabel_1,29,SpringLayout.SOUTH,lblNewLabel);
    sl_contentPane.putConstraint(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,52,SpringLayout.EAST,lblNewLabel);
    sl_contentPane.putConstraint(SpringLayout.EAST,196,lblNewLabel);
    contentPane.add(textField);
    textField.setColumns(10);
    
    passwordField = new JPasswordField();
    sl_contentPane.putConstraint(SpringLayout.NORTH,passwordField,26,textField);
    sl_contentPane.putConstraint(SpringLayout.WEST,lblNewLabel_1);
    sl_contentPane.putConstraint(SpringLayout.SOUTH,48,textField);
    sl_contentPane.putConstraint(SpringLayout.EAST,201,lblNewLabel_1);
    contentPane.add(passwordField);
    
    JButton btnNewButton = new JButton("登入");
    sl_contentPane.putConstraint(SpringLayout.SOUTH,btnNewButton,65,passwordField);
    contentPane.add(btnNewButton);
    
    JButton button = new JButton("重置");
    sl_contentPane.putConstraint(SpringLayout.NORTH,2,button);
    sl_contentPane.putConstraint(SpringLayout.EAST,-70,button);
    sl_contentPane.putConstraint(SpringLayout.NORTH,button,32,passwordField);
    sl_contentPane.putConstraint(SpringLayout.WEST,242,contentPane);
    button.setFont(new Font("宋體",19));
    contentPane.add(button);
    
    Checkbox checkbox = new Checkbox("記住密碼");
    sl_contentPane.putConstraint(SpringLayout.WEST,checkbox,textField);
    sl_contentPane.putConstraint(SpringLayout.SOUTH,-6,btnNewButton);
    contentPane.add(checkbox);
    
    Checkbox checkbox_1 = new Checkbox("隱身登入");
    sl_contentPane.putConstraint(SpringLayout.WEST,checkbox_1,35,checkbox);
    sl_contentPane.putConstraint(SpringLayout.SOUTH,checkbox);
    contentPane.add(checkbox_1);
  }
}

執行截圖:

java swing 建立一個簡單的QQ介面教程

以上這篇java swing 建立一個簡單的QQ介面教程就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。