Java案例分析
【案例11-3】 模拟QQ登录
2024-10-25 14 0
简介 1.案例描述 QQ是现实生活中常用的聊天工具,QQ登录界面看似小巧、简单,但其中涉及的内容却很多,对于初学者练习Java Swing工具的使用非常合适。本案例要求使用所学的Java Swing知识,模拟实现一个QQ登录界面。
【案例11-3】 模拟QQ登录
【案例介绍】
1.案例描述
QQ是现实生活中常用的聊天工具,QQ登录界面看似小巧、简单,但其中涉及的内容却很多,对于初学者练习Java Swing工具的使用非常合适。本案例要求使用所学的Java Swing知识,模拟实现一个QQ登录界面。
2.运行结果
运行结果
【案例目标】
l 学会分析“模拟QQ登录”程序实现的逻辑思路。
l 能够独立完成“模拟QQ登录”程序的源代码编写、编译及运行。
l 掌握Java Swing界面编程的应用。
【案例分析】
(1)首先,需要定义一些成员变量,如最小化、关闭、账号、密码、头像等,方便响应的逻辑实现。
(2)由于需要对账号、密码、头像等进行布局,故需要先对这些对象进行实例化。
(3)在对需要用到的文本框、图片等对象进行实例化过后,可以使用对象.setBounds()设置文本框、图片等组件的位置。
(4)接下来,对最小化、关闭、账号、密码、头像等添加监听事件。同时,对窗体也添加窗体拖动监听事件。
(5)最后,为最小化、关闭等编写点击时的执行逻辑。为账号、密码等设置点击、悬停等执行逻辑。
【案例实现】
Login.java
1 package chapter1103;
2 import java.awt.Color;
3 import java.awt.Cursor;
4 import java.awt.Font;
5 import java.awt.Point;
6 import java.awt.Toolkit;
7 import java.awt.event.FocusEvent;
8 import java.awt.event.FocusListener;
9 import java.awt.event.MouseEvent;
10 import java.awt.event.MouseListener;
11 import java.awt.event.MouseMotionListener;
12 import javax.swing.ImageIcon;
13 import javax.swing.JFrame;
14 import javax.swing.JLabel;
15 import javax.swing.JOptionPane;
16 import javax.swing.JPanel;
17 import javax.swing.JPasswordField;
18 import javax.swing.JTextField;
19 public class Login extends JFrame implements MouseListener {
20 JLabel bacgrangd, jan,bi,QQ,qq,tu;//gif,最小化,关闭,logo,QQ,头像
21 JLabel an1, an2, lie1, lie2;// 暗色块|线
22 JTextField user;// 账号
23 JPasswordField pass;// 密码
24 JPanel bgcolor;// 白
25 JLabel su1, mi1, ku1, ku2, gou1, gou2;// 缩略图
26 JLabel text1, text2, text3, text4, text5;//自动登录,记住密码,找回
27 //密码,注册账号,登录
28 static Point origin = new Point();// 变量,用于可拖动窗体
29 int a = 0, b = 0, c = 0, d = 0;// 控制线
30 int f = 0, g = 0, h = 0, j = 0;// 控制√
31 JLabel submit, ma;// 背景
32 public Login() {
33 //实例化
34 bacgrangd = new JLabel(new ImageIcon("images/1.gif"));
35 jan = new JLabel(new ImageIcon("images/最小化.png"));
36 bi = new JLabel(new ImageIcon("images/关闭.png"));
37 QQ = new JLabel(new ImageIcon("imagesqq.png"));
38 qq = new JLabel("QQ");
39 an1 = new JLabel();
40 an2 = new JLabel();// 暗调
41 tu = new JLabel(new ImageIcon("images/头像.png"));
42 user = new JTextField();
43 pass = new JPasswordField();
44 su1 = new JLabel(new ImageIcon("images/qq (1).png"));
45 mi1 = new JLabel(new ImageIcon("images/密码.png"));
46 lie1 = new JLabel(new ImageIcon("images/直线2.png"));
47 lie2 = new JLabel(new ImageIcon("images/直线2.png"));
48 bgcolor = new JPanel();
49 ku1 = new JLabel(new ImageIcon("images/框框.png"));
50 ku2 = new JLabel(new ImageIcon("images/框框.png"));
51 gou1 = new JLabel(new ImageIcon("images/对勾.png"));
52 gou2 = new JLabel(new ImageIcon("images/对勾.png"));
53 text1 = new JLabel("自动登录");
54 text2 = new JLabel("记住密码");
55 text3 = new JLabel("找回密码");
56 text4 = new JLabel("注册账号");
57 text5 = new JLabel("登录");
58 submit = new JLabel();
59 ma = new JLabel(new ImageIcon("images/二维码.png"));
60 //位置
61 bacgrangd.setBounds(-35, -123, 500, 250);
62 jan.setBounds(364, 2, 32, 32);
63 bi.setBounds(396, 3, 32, 32);
64 QQ.setBounds(10, 10, 32, 32);
65 qq.setBounds(50, 5, 45, 45);
66 an1.setBounds(361, 0, 35, 35);
67 an2.setBounds(395, 0, 35, 35);
68 tu.setBounds(170, 80, 90, 85);
69 user.setBounds(130, 160, 180, 40);
70 pass.setBounds(130, 200, 180, 40);
71 su1.setBounds(100, 170, 20, 20);
72 mi1.setBounds(100, 210, 20, 20);
73 lie1.setBounds(100, 190, 240, 10);
74 lie2.setBounds(100, 230, 240, 10);
75 bgcolor.setBounds(0, 125, 500, 300);
76 ku1.setBounds(100, 250, 20, 20);
77 ku2.setBounds(190, 250, 20, 20);
78 gou1.setBounds(106, 255, 10, 10);
79 gou2.setBounds(196, 255, 10, 10);
80 text1.setBounds(125, 250, 80, 20);
81 text2.setBounds(215, 250, 80, 20);
82 text3.setBounds(288, 250, 80, 20);
83 text4.setBounds(15, 300, 80, 20);
84 text5.setBounds(206, 285, 80, 20);
85 submit.setBounds(100, 280, 242, 35);
86 ma.setBounds(385, 290, 30, 30);
87 //属性
88 qq.setFont(new Font("微软雅黑", 1, 25));
89 qq.setForeground(Color.white);
90 an1.setBackground(new Color(0, 0, 0, 0.3f));
91 an2.setBackground(new Color(0, 0, 0, 0.3f));
92 bgcolor.setBackground(new Color(255, 255, 255));
93 user.setForeground(Color.gray);
94 user.setText("QQ号码/手机/邮箱");
95 user.setOpaque(false);// 透明背景
96 user.setBorder(null);// 去掉边框
97 // 框内文字样式
98 user.setFont(new Font("微软雅黑", Font.PLAIN, 16));
99 // 框内文字样式
100 pass.setFont(new Font("微软雅黑", Font.PLAIN, 16));
101 pass.setBorder(null);// 去掉边框
102 pass.setOpaque(false);// 透明背景
103 pass.setForeground(Color.gray);
104 pass.setText("密码");
105 pass.setEchoChar((char) 0);// 让密码显示出来
106 text1.setFont(new Font("微软雅黑", 0, 12));
107 text2.setFont(new Font("微软雅黑", 0, 12));
108 text3.setFont(new Font("微软雅黑", 0, 12));
109 text4.setFont(new Font("微软雅黑", 0, 12));
110 text5.setFont(new Font("微软雅黑", 0, 15));
111 text1.setForeground(new Color(170, 170, 170));
112 text2.setForeground(new Color(170, 170, 170));
113 text3.setForeground(new Color(170, 170, 170));
114 text4.setForeground(new Color(170, 170, 170));
115 text5.setForeground(Color.white);
116 gou1.setVisible(false);
117 gou2.setVisible(false);
118 submit.setBackground(new Color(5, 186, 251));
119 submit.setOpaque(true);
120 text3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
121 text4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
122 //事件区域
123 jan.addMouseListener(this);
124 bi.addMouseListener(this);
125 user.addMouseListener(this);
126 pass.addMouseListener(this);
127 text1.addMouseListener(this);
128 text2.addMouseListener(this);
129 text3.addMouseListener(this);
130 text4.addMouseListener(this);
131 ku1.addMouseListener(this);
132 ku2.addMouseListener(this);
133 submit.addMouseListener(this);
134 ma.addMouseListener(this);
135 this.addMouseListener(this);
136 // 窗体拖动事件
137 this.addMouseMotionListener(new MouseMotionListener() {
138 public void mouseMoved(MouseEvent e) {
139 }
140 public void mouseDragged(MouseEvent e) {
141 Point p = getLocation();
142 setLocation(p.x + e.getX()-origin.x, p.y + e.getY()-origin.y);
143 }
144 });
145 user.addFocusListener(new FocusListener() {
146 public void focusLost(FocusEvent e) {// 失去焦点
147 su1.setIcon(new javax.swing.ImageIcon("images/qq (1).png"));
148 lie1.setIcon(new javax.swing.ImageIcon("images/直线2.png"));
149 c = 0;
150 // 判断是否为空(为了设置默认提示语)
151 if (user.getText().isEmpty()) {
152 user.setForeground(Color.gray);
153 user.setText("QQ号码/手机/邮箱");
154 }
155 }
156 // 得到焦点
157 public void focusGained(FocusEvent e) {
158 user.setForeground(Color.black);
159 lie1.setIcon(new javax.swing.ImageIcon("images/直线3.png"));
160 a = 1;
161 c = 1;
162 b = 0;
163 su1.setIcon(new javax.swing.ImageIcon("images/qq(2).png"));
164 if (user.getText().equals("QQ号码/手机/邮箱")) {
165 user.setText("");
166 } else {
167 user.setText(user.getText());
168 user.selectAll();
169 }
170 }
171 });
172 pass.addFocusListener(new FocusListener() {
173 // 失去焦点
174 public void focusLost(FocusEvent e) {
175 // 失去焦点换图片
176 lie2.setIcon(new javax.swing.ImageIcon("images/直2.png"));
177 mi1.setIcon(new javax.swing.ImageIcon("images/密码.png"));
178 d = 0;
179 if (pass.getText().isEmpty()) {
180 pass.setForeground(Color.gray);
181 pass.setText("密码");
182 pass.setEchoChar((char) 0);// 让密码显示出来
183 }
184 }
185 public void focusGained(FocusEvent e) {// 得到焦点
186 mi1.setIcon(new javax.swing.ImageIcon("images/密码"+
187 " (1).png"));
188 lie2.setIcon(new javax.swing.ImageIcon("images/直线"+
189 "3.png"));
190 b = 1;
191 a = 0;
192 d = 1;
193 pass.setForeground(Color.black);
194 pass.setEchoChar('*');// 让用户输入看不见
195 if (pass.getText().equals("密码")) {
196 pass.setText("");
197 } else {
198 pass.setText(pass.getText());
199 }
200 }
201 });
202 this.setLayout(null);// 布局
203 this.add(jan);
204 this.add(bi);
205 this.add(qq);
206 this.add(QQ);
207 this.add(an1);
208 this.add(an2);
209 this.add(tu);
210 this.add(lie1);
211 this.add(lie2);
212 this.add(user);
213 this.add(pass);
214 this.add(su1);
215 this.add(mi1);
216 this.add(gou1);
217 this.add(gou2);
218 this.add(ku1);
219 this.add(ku2);
220 this.add(text1);
221 this.add(text2);
222 this.add(text3);
223 this.add(text4);
224 this.add(text5);
225 this.add(submit);
226 this.add(ma);
227 this.add(bgcolor);
228 this.add(bacgrangd);
229 this.setSize(430, 330);
230 this.setIconImage(Toolkit.getDefaultToolkit().createImage("images"+
231 "/透明照片.png"));// 窗体图标
232 this.setLocationRelativeTo(null);// 保持居中
233 this.setUndecorated(true);// 去顶部
234 this.setFocusable(true);// 面板首先获得焦点
235 this.setBackground(new Color(255, 255, 255));// 背景颜色
236 this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
237 this.setAlwaysOnTop(true);// 最顶层
238 this.setVisible(true);// 显示
239 }
240 public static void main(String[] args) {
241 new Login();
242 }
243 // 点击不恢复
244 public void mouseClicked(MouseEvent e) {
245 }
246 // 点击后
247 public void mousePressed(MouseEvent e) {
248 if (e.getSource() == jan) {
249 setExtendedState(JFrame.ICONIFIED);
250 } else if (e.getSource() == this) {
251 origin.x = e.getX();
252 origin.y = e.getY();
253 } else if (e.getSource() == bi) {
254 System.exit(0);
255 } else if (e.getSource() == ku1 || e.getSource() == text1) {
256 if (f == 0) {
257 gou1.setVisible(true);
258 g = 1;
259 f = 1;
260 } else if (g == 1) {
261 gou1.setVisible(false);
262 f = 0;
263 g = 0;
264 }
265 } else if (e.getSource() == ku2 || e.getSource() == text2) {
266 if (h == 0) {
267 gou2.setVisible(true);
268 j = 1;
269 h = 1;
270 } else if (j == 1) {
271 gou2.setVisible(false);
272 h = 0;
273 j = 0;
274 }
275 } else if (e.getSource() == submit || e.getSource() == text5) {
276 text5.setFont(new Font("微软雅黑", 0, 14));
277 dispose();
278 String users = user.getText();
279 String password = pass.getText();
280 if (users.equals("itcast") && password.equals("123")) {
281 //new Table();//打开新的主界面如果要关闭登录界面可以写dispose();
282 } else {
283 JOptionPane.showMessageDialog(null, "用户名:itcast,密"+
284 "码:123,您并未设置打开界面!");
285 new Login();
286 }
287 }
288 }
289 // 点击时
290 public void mouseReleased(MouseEvent e) {
291 if (e.getSource() == submit || e.getSource() == text5) {
292 text5.setFont(new Font("微软雅黑", 0, 15));
293 }
294 }
295 // 悬停
296 public void mouseEntered(MouseEvent e) {
297 if (e.getSource() == jan) {
298 an1.setOpaque(true);
299 } else if (e.getSource() == bi) {
300 an2.setOpaque(true);
301 } else if (e.getSource() == user) {
302 if (a == 0 && c == 0) {
303 lie1.setIcon(new javax.swing.ImageIcon("images/直线4.png"));
304 }
305 } else if (e.getSource() == pass) {
306 if (b == 0 && d == 0) {
307 lie2.setIcon(new javax.swing.ImageIcon("images/直线4.png"));
308 }
309 } else if (e.getSource() == text3) {
310 text3.setForeground(Color.GRAY);
311 } else if (e.getSource() == text4) {
312 text4.setForeground(Color.GRAY);
313 } else if (e.getSource() == ma) {
314 ma.setIcon(new javax.swing.ImageIcon("images/二维码2.png"));
315 }
316 }
317 public void mouseExited(MouseEvent e) {// 悬停后
318 if (e.getSource() == jan) {
319 an1.setOpaque(false);
320 } else if (e.getSource() == bi) {
321 an2.setOpaque(false);
322 } else if (e.getSource() == user) {
323 if (a == 0) {
324 lie1.setIcon(new javax.swing.ImageIcon("images/直线2.png"));
325 }
326 } else if (e.getSource() == pass) {
327 if (b == 0) {
328 lie2.setIcon(new javax.swing.ImageIcon("images/直线2.png"));
329 }
330 } else if (e.getSource() == text3) {
331 text3.setForeground(new Color(170, 170, 170));
332 } else if (e.getSource() == text4) {
333 text4.setForeground(new Color(170, 170, 170));
334 } else if (e.getSource() == ma) {
335 ma.setIcon(new javax.swing.ImageIcon("images/二码.png"));
336 }
337 }
338 }
上述代码中,第19-30行代码,定义了一些成员变量,方便响应的逻辑实现。第33-58行代码,对一些图片对象进行实例化。第60-85行,设置图片、文本框等的位置。第87-120行,设置各个文本框,文字等的样式。第122-134行,为各个文本框、按钮等设置监听事件。第136-143行,为窗体拖动事件设置窗体监听。第144-170,为账号文本框设置鼠标聚焦事件。第171-200,为密码文本框设置鼠标聚焦事件。第201-227行,将各个按钮,图片文本框对象放入容器内。第228-238,对界面进行布局。mouseClicked()方法中编写了按钮,文本框,文字等点击不回复的逻辑。mousePressed()方法中编写了按钮,文本框,文字等点击后的逻辑。mouseReleased()方法中编写了按钮,文本框,文字等点击时的逻辑。mouseEntered()方法中编写了按钮,文本框,文字等悬停时的逻辑。mouseExited()方法中编写了按钮,文本框,文字等悬停后的逻辑。