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



