1. 程式人生 > >java實現簡單的QQ登入介面

java實現簡單的QQ登入介面

java在圖形介面,不是太強項,但不是不可以做,它的開源是very nice!

實現程式碼如下(想實現完美的介面,可能要更多coding的支援):

package com.ts.x.swing;

import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JRootPane;
import javax.swing.JTextField;

public class QQ extends JFrame{
	
	private static final long serialVersionUID = -6788045638380819221L;
	//使用者名稱
	private JTextField ulName;
	//密碼
	private JPasswordField ulPasswd;
	//小容器
	private JLabel j1;
	private JLabel j2;
	private JLabel j3;
	private JLabel j4;
	//小按鈕
	private JButton b1;
	private JButton b2;
	private JButton b3;
	//複選框
	private JCheckBox c1;
	private JCheckBox c2;
	//列表框
	private JComboBox<String> cb1;
	/**
	 * 初始化QQ登入頁面
	 * */
	public QQ(){
		//設定登入視窗標題
		this.setTitle("QQ登入");
		//去掉視窗的裝飾(邊框)
//		this.setUndecorated(true); 
		//採用指定的視窗裝飾風格
		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("D:/logo.png").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("D:/bgimg.png").getImage();
		j1.setIcon(new ImageIcon(img1));
		j1.setBounds(0, 0, 355, 265);
		//qq頭像設定
		j2 = new JLabel();
		Image img2 = new ImageIcon("D:/hdimg.png").getImage();
		j2.setIcon(new ImageIcon(img2));
		j2.setBounds(40, 95, 50, 53);
		//使用者名稱輸入框
		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);
		//使用者登陸狀態選擇
		cb1 = new JComboBox<String>();
		cb1.addItem("線上");
		cb1.addItem("隱身");
		cb1.addItem("離開");
		cb1.setBounds(40, 150, 55, 20);
		//登陸按鈕
		b1 = new JButton("登入");
		//設定字型和顏色和手形指標
		b1.setFont(new Font("宋體", Font.PLAIN, 12));
		b1.setForeground(Color.RED);
		b1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		b1.setBounds(280, 200, 65, 20);
		//給按鈕新增
		b1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String cmd = e.getActionCommand();
				if("登入".equals(cmd)){
					String username = ulName.getText();
					String userpassword = ulPasswd.getText();
					if(username.equals("tskk") && userpassword.equals("123456")){
						JOptionPane.showConfirmDialog(null, "登入成功");
					}else{
						JOptionPane.showConfirmDialog(null, "登入失敗");
					}
				}
			}
		});
		//多賬號
		b2 = new JButton("多賬號");
		b2.setBounds(5, 200, 75, 20);
		//設定
		b3 = new JButton("設定");
		b3.setBounds(100, 200, 65, 20);
		//所有元件用容器裝載
		j1.add(j2);
		j1.add(j3);
		j1.add(j4);
		j1.add(c1);
		j1.add(c2);
		j1.add(cb1);
		j1.add(b1);
		j1.add(b2);
		j1.add(b3);
		container.add(j1);
		container.add(ulName);
		container.add(ulPasswd);
	}
	public static void main(String[] args) {
		new QQ();
	}
}

執行結果介面為: