【案例11-3】 模擬QQ登錄
【案例介紹】
1.案例描述
QQ是現(xiàn)實(shí)生活中常用的聊天工具,QQ登錄界面看似小巧、簡(jiǎn)單,但其中涉及的內(nèi)容卻很多,對(duì)于初學(xué)者練習(xí)Java Swing工具的使用非常合適。本案例要求使用所學(xué)的Java Swing知識(shí),模擬實(shí)現(xiàn)一個(gè)QQ登錄界面。
2.運(yùn)行結(jié)果
?
運(yùn)行結(jié)果
【案例分析】
(1)首先,需要定義一些成員變量,如最小化、關(guān)閉、賬號(hào)、密碼、頭像等,方便響應(yīng)的邏輯實(shí)現(xiàn)。
(2)由于需要對(duì)賬號(hào)、密碼、頭像等進(jìn)行布局,故需要先對(duì)這些對(duì)象進(jìn)行實(shí)例化。
(3)在對(duì)需要用到的文本框、圖片等對(duì)象進(jìn)行實(shí)例化過(guò)后,可以使用對(duì)象.setBounds()設(shè)置文本框、圖片等組件的位置。
(4)接下來(lái),對(duì)最小化、關(guān)閉、賬號(hào)、密碼、頭像等添加監(jiān)聽事件。同時(shí),對(duì)窗體也添加窗體拖動(dòng)監(jiān)聽事件。
(5)最后,為最小化、關(guān)閉等編寫點(diǎn)擊時(shí)的執(zhí)行邏輯。為賬號(hào)、密碼等設(shè)置點(diǎn)擊、懸停等執(zhí)行邏輯。
【案例實(shí)現(xiàn)】
Login.java文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-479671.html
- package chapter1103;
- import java.awt.Color;
- ????import java.awt.Cursor;
- ????import java.awt.Font;
- ????import java.awt.Point;
- ????import java.awt.Toolkit;
- ????import java.awt.event.FocusEvent;
- ????import java.awt.event.FocusListener;
- ????import java.awt.event.MouseEvent;
- ????import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- public class Login extends JFrame implements MouseListener {
- ??? JLabel bacgrangd, jan,bi,QQ,qq,tu;//gif,最小化,關(guān)閉,logo,QQ,頭像
- ??? JLabel an1, an2, lie1, lie2;// 暗色塊|線
- ??? JTextField user;// 賬號(hào)
- ??? JPasswordField pass;// 密碼
- ??? JPanel bgcolor;// 白
- ??? JLabel su1, mi1, ku1, ku2, gou1, gou2;// 縮略圖
- ??? JLabel text1, text2, text3, text4, text5;//自動(dòng)登錄,記住密碼,找回
- //密碼,注冊(cè)賬號(hào),登錄
- ??? static Point origin = new Point();// 變量,用于可拖動(dòng)窗體
- ??? int a = 0, b = 0, c = 0, d = 0;// 控制線
- ??? int f = 0, g = 0, h = 0, j = 0;// 控制√
- ??? JLabel submit, ma;// 背景
- ??? public Login() {
- ??????? //實(shí)例化
- ??????? bacgrangd = new JLabel(new ImageIcon("images/1.gif"));
- ??????? jan = new JLabel(new ImageIcon("images/最小化.png"));
- ??????? bi = new JLabel(new ImageIcon("images/關(guān)閉.png"));
- ??????? QQ = new JLabel(new ImageIcon("imagesqq.png"));
- ??????? qq = new JLabel("QQ");
- ??????? an1 = new JLabel();
- ??????? an2 = new JLabel();// 暗調(diào)
- ??????? tu = new JLabel(new ImageIcon("images/頭像.png"));
- ??????? user = new JTextField();
- ??????? pass = new JPasswordField();
- ??????? su1 = new JLabel(new ImageIcon("images/qq (1).png"));
- ??????? mi1 = new JLabel(new ImageIcon("images/密碼.png"));
- ??????? lie1 = new JLabel(new ImageIcon("images/直線2.png"));
- ??????? lie2 = new JLabel(new ImageIcon("images/直線2.png"));
- ??????? bgcolor = new JPanel();
- ??????? ku1 = new JLabel(new ImageIcon("images/框框.png"));
- ??????? ku2 = new JLabel(new ImageIcon("images/框框.png"));
- ??????? gou1 = new JLabel(new ImageIcon("images/對(duì)勾.png"));
- ??????? gou2 = new JLabel(new ImageIcon("images/對(duì)勾.png"));
- ??????? text1 = new JLabel("自動(dòng)登錄");
- ??????? text2 = new JLabel("記住密碼");
- ??????? text3 = new JLabel("找回密碼");
- ??????? text4 = new JLabel("注冊(cè)賬號(hào)");
- ??????? text5 = new JLabel("登錄");
- ??????? submit = new JLabel();
- ??????? ma = new JLabel(new ImageIcon("images/二維碼.png"));
- ??????? //位置
- ??????? bacgrangd.setBounds(-35, -123, 500, 250);
- ??????? jan.setBounds(364, 2, 32, 32);
- ??????? bi.setBounds(396, 3, 32, 32);
- ??????? QQ.setBounds(10, 10, 32, 32);
- ??????? qq.setBounds(50, 5, 45, 45);
- ??????? an1.setBounds(361, 0, 35, 35);
- ??????? an2.setBounds(395, 0, 35, 35);
- ??????? tu.setBounds(170, 80, 90, 85);
- ??????? user.setBounds(130, 160, 180, 40);
- ??????? pass.setBounds(130, 200, 180, 40);
- ??????? su1.setBounds(100, 170, 20, 20);
- ??????? mi1.setBounds(100, 210, 20, 20);
- ??????? lie1.setBounds(100, 190, 240, 10);
- ??????? lie2.setBounds(100, 230, 240, 10);
- ??????? bgcolor.setBounds(0, 125, 500, 300);
- ??????? ku1.setBounds(100, 250, 20, 20);
- ??????? ku2.setBounds(190, 250, 20, 20);
- ??????? gou1.setBounds(106, 255, 10, 10);
- ??????? gou2.setBounds(196, 255, 10, 10);
- ??????? text1.setBounds(125, 250, 80, 20);
- ??????? text2.setBounds(215, 250, 80, 20);
- ??????? text3.setBounds(288, 250, 80, 20);
- ??????? text4.setBounds(15, 300, 80, 20);
- ??????? text5.setBounds(206, 285, 80, 20);
- ??????? submit.setBounds(100, 280, 242, 35);
- ??????? ma.setBounds(385, 290, 30, 30);
- ??????? //屬性
- ??????? qq.setFont(new Font("微軟雅黑", 1, 25));
- ??????? qq.setForeground(Color.white);
- ??????? an1.setBackground(new Color(0, 0, 0, 0.3f));
- ??????? an2.setBackground(new Color(0, 0, 0, 0.3f));
- ??????? bgcolor.setBackground(new Color(255, 255, 255));
- ??????? user.setForeground(Color.gray);
- ??????? user.setText("QQ號(hào)碼/手機(jī)/郵箱");
- ??????? user.setOpaque(false);// 透明背景
- ??????? user.setBorder(null);// 去掉邊框
- ?????????// 框內(nèi)文字樣式
- ??????? user.setFont(new Font("微軟雅黑", Font.PLAIN, 16));
- ?????????// 框內(nèi)文字樣式
- ??????? pass.setFont(new Font("微軟雅黑", Font.PLAIN, 16));
- ??????? pass.setBorder(null);// 去掉邊框
- ??????? pass.setOpaque(false);// 透明背景
- ??????? pass.setForeground(Color.gray);
- ??????? pass.setText("密碼");
- ??????? pass.setEchoChar((char) 0);// 讓密碼顯示出來(lái)
- ??????? text1.setFont(new Font("微軟雅黑", 0, 12));
- ??????? text2.setFont(new Font("微軟雅黑", 0, 12));
- ??????? text3.setFont(new Font("微軟雅黑", 0, 12));
- ??????? text4.setFont(new Font("微軟雅黑", 0, 12));
- ??????? text5.setFont(new Font("微軟雅黑", 0, 15));
- ??????? text1.setForeground(new Color(170, 170, 170));
- ??????? text2.setForeground(new Color(170, 170, 170));
- ??????? text3.setForeground(new Color(170, 170, 170));
- ??????? text4.setForeground(new Color(170, 170, 170));
- ??????? text5.setForeground(Color.white);
- ??????? gou1.setVisible(false);
- ??????? gou2.setVisible(false);
- ??????? submit.setBackground(new Color(5, 186, 251));
- ??????? submit.setOpaque(true);
- ??? ?text3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- ??? ?text4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
- ??????? //事件區(qū)域
- ??????? jan.addMouseListener(this);
- ??????? bi.addMouseListener(this);
- ??????? user.addMouseListener(this);
- ??????? pass.addMouseListener(this);
- ??????? text1.addMouseListener(this);
- ??????? text2.addMouseListener(this);
- ??????? text3.addMouseListener(this);
- ??????? text4.addMouseListener(this);
- ??????? ku1.addMouseListener(this);
- ??????? ku2.addMouseListener(this);
- ??????? submit.addMouseListener(this);
- ??????? ma.addMouseListener(this);
- ??????? this.addMouseListener(this);
- ?????????// 窗體拖動(dòng)事件
- ?this.addMouseMotionListener(new MouseMotionListener() {
- ?????????? public void mouseMoved(MouseEvent e) {
- ??????? }
- ??? public void mouseDragged(MouseEvent e) {
- ??? ???Point p = getLocation();
- ????????setLocation(p.x + e.getX()-origin.x, p.y + e.getY()-origin.y);
- ??? }
- });
- user.addFocusListener(new FocusListener() {
- ?????public void focusLost(FocusEvent e) {// 失去焦點(diǎn)
- ??????? ?su1.setIcon(new javax.swing.ImageIcon("images/qq (1).png"));
- ??????? ?lie1.setIcon(new javax.swing.ImageIcon("images/直線2.png"));
- ?????????? ??c = 0;
- ??????????????// 判斷是否為空(為了設(shè)置默認(rèn)提示語(yǔ))
- ??? ?????????if (user.getText().isEmpty()) {
- ?????????????????? user.setForeground(Color.gray);
- ?????????????????? user.setText("QQ號(hào)碼/手機(jī)/郵箱");
- ?????????????? }
- ?????????? }
- ????????// 得到焦點(diǎn)
- ??? ???public void focusGained(FocusEvent e) {
- ??????? ?user.setForeground(Color.black);
- ??????? ?lie1.setIcon(new javax.swing.ImageIcon("images/直線3.png"));
- ?????????????? a = 1;
- ?????????????? c = 1;
- ?????????????? b = 0;
- ??????? ??su1.setIcon(new javax.swing.ImageIcon("images/qq(2).png"));
- ?????????????? if (user.getText().equals("QQ號(hào)碼/手機(jī)/郵箱")) {
- ?????????????????? user.setText("");
- ?????????????? } else {
- ?????????????????? user.setText(user.getText());
- ?????????????????? user.selectAll();
- ?????????????? }
- ?????????? }
- ??????? });
- ??????? pass.addFocusListener(new FocusListener() {
- ????????????// 失去焦點(diǎn)
- ??????? ???public void focusLost(FocusEvent e) {
- ????????????// 失去焦點(diǎn)換圖片
- ??????? ???lie2.setIcon(new javax.swing.ImageIcon("images/直2.png"));
- ??????? ???mi1.setIcon(new javax.swing.ImageIcon("images/密碼.png"));
- ?????????????? d = 0;
- ?????????????? if (pass.getText().isEmpty()) {
- ?????????????????? pass.setForeground(Color.gray);
- ?????????????????? pass.setText("密碼");
- ?????????????????? pass.setEchoChar((char) 0);// 讓密碼顯示出來(lái)
- ?????????????? }
- ?????????? }
- ??????? public void focusGained(FocusEvent e) {// 得到焦點(diǎn)
- ?????????????? mi1.setIcon(new javax.swing.ImageIcon("images/密碼"+
- ??????????????????" (1).png"));
- ?????????????? lie2.setIcon(new javax.swing.ImageIcon("images/直線"+
- ??????????????????"3.png"));
- ?????????????? b = 1;
- ?????????????? a = 0;
- ?????????????? d = 1;
- ?????????????? pass.setForeground(Color.black);
- ?????????????? pass.setEchoChar('*');// 讓用戶輸入看不見
- ?????????????? if (pass.getText().equals("密碼")) {
- ?????????????????? pass.setText("");
- ?????????????? } else {
- ?????????????????? pass.setText(pass.getText());
- ?????????????? }
- ?????????? }
- ??????? });
- ??????? this.setLayout(null);// 布局
- ??????? this.add(jan);
- ??????? this.add(bi);
- ??????? this.add(qq);
- ??????? this.add(QQ);
- ??????? this.add(an1);
- ??????? this.add(an2);
- ??????? this.add(tu);
- ??????? this.add(lie1);
- ??????? this.add(lie2);
- ??????? this.add(user);
- ??????? this.add(pass);
- ??????? this.add(su1);
- ??????? this.add(mi1);
- ??????? this.add(gou1);
- ??????? this.add(gou2);
- ??????? this.add(ku1);
- ??????? this.add(ku2);
- ??????? this.add(text1);
- ??????? this.add(text2);
- ??????? this.add(text3);
- ??????? this.add(text4);
- ??????? this.add(text5);
- ??????? this.add(submit);
- ??????? this.add(ma);
- ??????? this.add(bgcolor);
- ??????? this.add(bacgrangd);
- ??????? this.setSize(430, 330);
- this.setIconImage(Toolkit.getDefaultToolkit().createImage("images"+
- ????"/透明照片.png"));// 窗體圖標(biāo)
- ??????? this.setLocationRelativeTo(null);// 保持居中
- ??????? this.setUndecorated(true);// 去頂部
- ??????? this.setFocusable(true);// 面板首先獲得焦點(diǎn)
- ??????? this.setBackground(new Color(255, 255, 255));// 背景顏色
- ??? ??? this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
- ??????? this.setAlwaysOnTop(true);// 最頂層
- ??????? this.setVisible(true);// 顯示
- ??? }
- ??? public static void main(String[] args) {
- ??????? new Login();
- ??? }
- ????// 點(diǎn)擊不恢復(fù)
- ??? public void mouseClicked(MouseEvent e) {
- ??? }
- ????// 點(diǎn)擊后
- ??? public void mousePressed(MouseEvent e) {
- ??????? if (e.getSource() == jan) {
- ?????????? setExtendedState(JFrame.ICONIFIED);
- ??????? } else if (e.getSource() == this) {
- ?????????? origin.x = e.getX();
- ?????????? origin.y = e.getY();
- ??????? } else if (e.getSource() == bi) {
- ?????????? System.exit(0);
- ??????? } else if (e.getSource() == ku1 || e.getSource() == text1) {
- ?????????? if (f == 0) {
- ?????????????? gou1.setVisible(true);
- ?????????????? g = 1;
- ?????????????? f = 1;
- ?????????? } else if (g == 1) {
- ?????????????? gou1.setVisible(false);
- ?????????????? f = 0;
- ?????????????? g = 0;
- ?????????? }
- ??????? } else if (e.getSource() == ku2 || e.getSource() == text2) {
- ?????????? if (h == 0) {
- ?????????????? gou2.setVisible(true);
- ?????????????? j = 1;
- ?????????????? h = 1;
- ?????????? } else if (j == 1) {
- ?????????????? gou2.setVisible(false);
- ?????????????? h = 0;
- ?????????????? j = 0;
- ??? ??????? }
- ??? ??} else if (e.getSource() == submit || e.getSource() == text5) {
- ?????????? text5.setFont(new Font("微軟雅黑", 0, 14));
- ?????????? dispose();
- ?????????? String users = user.getText();
- ?????????? String password = pass.getText();
- ?????????? if (users.equals("itcast") && password.equals("123")) {
- ??????? ???//new Table();//打開新的主界面如果要關(guān)閉登錄界面可以寫dispose();
- ?????????? } else {
- ?????????? JOptionPane.showMessageDialog(null, "用戶名:itcast,密"+
- ???????????????????"碼:123,您并未設(shè)置打開界面!");
- ?????????????? new Login();
- ?????????? }
- ??????? }
- ??? }
- ?????// 點(diǎn)擊時(shí)
- ??? public void mouseReleased(MouseEvent e) {
- ??????? if (e.getSource() == submit || e.getSource() == text5) {
- ?????????? text5.setFont(new Font("微軟雅黑", 0, 15));
- ??????? }
- ??? }
- ?????// 懸停
- ??? public void mouseEntered(MouseEvent e) {
- ??????? if (e.getSource() == jan) {
- ?????????? an1.setOpaque(true);
- ??????? } else if (e.getSource() == bi) {
- ?????????? an2.setOpaque(true);
- ??????? } else if (e.getSource() == user) {
- ?????????? if (a == 0 && c == 0) {
- ??????? ?lie1.setIcon(new javax.swing.ImageIcon("images/直線4.png"));
- ?????????? }
- ??????? } else if (e.getSource() == pass) {
- ?????????? if (b == 0 && d == 0) {
- ??????? ?lie2.setIcon(new javax.swing.ImageIcon("images/直線4.png"));
- ?????????? }
- ??????? } else if (e.getSource() == text3) {
- ?????????? text3.setForeground(Color.GRAY);
- ??????? } else if (e.getSource() == text4) {
- ?????????? text4.setForeground(Color.GRAY);
- ??????? } else if (e.getSource() == ma) {
- ??????? ??ma.setIcon(new javax.swing.ImageIcon("images/二維碼2.png"));
- ??????? }
- ??? }
- ??? public void mouseExited(MouseEvent e) {// 懸停后
- ??????? if (e.getSource() == jan) {
- ?????????? an1.setOpaque(false);
- ??????? } else if (e.getSource() == bi) {
- ?????????? an2.setOpaque(false);
- ??????? } else if (e.getSource() == user) {
- ?????????? if (a == 0) {
- ??????? ?lie1.setIcon(new javax.swing.ImageIcon("images/直線2.png"));
- ?????????? }
- ??????? } else if (e.getSource() == pass) {
- ?????????? if (b == 0) {
- ??????? ?lie2.setIcon(new javax.swing.ImageIcon("images/直線2.png"));
- ?????????? }
- ??????? } else if (e.getSource() == text3) {
- ?????????? text3.setForeground(new Color(170, 170, 170));
- ??????? } else if (e.getSource() == text4) {
- ?????????? text4.setForeground(new Color(170, 170, 170));
- ??????? } else if (e.getSource() == ma) {
- ?????????? ma.setIcon(new javax.swing.ImageIcon("images/二碼.png"));
- ??????? }
- ??? }
- }
上述代碼中,第19-30行代碼,定義了一些成員變量,方便響應(yīng)的邏輯實(shí)現(xiàn)。第33-58行代碼,對(duì)一些圖片對(duì)象進(jìn)行實(shí)例化。第60-85行,設(shè)置圖片、文本框等的位置。第87-120行,設(shè)置各個(gè)文本框,文字等的樣式。第122-134行,為各個(gè)文本框、按鈕等設(shè)置監(jiān)聽事件。第136-143行,為窗體拖動(dòng)事件設(shè)置窗體監(jiān)聽。第144-170,為賬號(hào)文本框設(shè)置鼠標(biāo)聚焦事件。第171-200,為密碼文本框設(shè)置鼠標(biāo)聚焦事件。第201-227行,將各個(gè)按鈕,圖片文本框?qū)ο蠓湃肴萜鲀?nèi)。第228-238,對(duì)界面進(jìn)行布局。mouseClicked()方法中編寫了按鈕,文本框,文字等點(diǎn)擊不回復(fù)的邏輯。mousePressed()方法中編寫了按鈕,文本框,文字等點(diǎn)擊后的邏輯。mouseReleased()方法中編寫了按鈕,文本框,文字等點(diǎn)擊時(shí)的邏輯。mouseEntered()方法中編寫了按鈕,文本框,文字等懸停時(shí)的邏輯。mouseExited()方法中編寫了按鈕,文本框,文字等懸停后的邏輯。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-479671.html
到了這里,關(guān)于模擬QQ登錄-課后程序(JAVA基礎(chǔ)案例教程-黑馬程序員編著-第十一章-課后作業(yè))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!