1 import java.awt.*;
2 import javax.swing.*;
3 import javax.swing.JFrame;
4 import java.awt.event.WindowListener;
5 import java.awt.event.WindowEvent;
6 import java.awt.event.WindowAdapter;
7 import java.awt.event.ActionListener;
8 import java.awt.event.ActionEvent;
9 class QQ extends JFrame //继承JFrame
10 {
11 QQ() //构造函数
12 {
13 display();//调用display()函数
14 }
15 JButton btn1;
16 JButton btn2;
17 JTextField jtf1;
18 JPasswordField jpf1; //密码文本
19 class MyListener extends WindowAdapter //适配器 是扩展 不需要覆盖WindowAdapter中的所有方法 功能代码较多
20 {
21 public void windowClosing(WindowEvent e)
22 {
23 System.out.println("windowClosing");
24 int res = JOptionPane.showConfirmDialog(null,"是否退出程序应用","提示", JOptionPane.YES_NO_OPTION); //弹出消息框
25 System.out.println("res =" + res);
26 if(res == 0)
27 {
28 System.out.println("退出");
29 System.exit(1);// 退出
30 System.out.println("退出1"); //不输出 因为exit先执行
31 }
32 else if(res == 1)
33 {
34 System.out.println("不退出");
35 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
36 //System.exit(1);// 退出
37 // 不退出
38 }
39 }
40 }
41 class BtnAction implements ActionListener //btn事件
42 {
43 public void actionPerformed(ActionEvent e)
44 {
45 System.out.println("actionPerformed");
46 if(jtf1.getText().equals("jk") && jpf1.getText().equals("jk"))
47 {
48 System.out.println("OK");
49 dispose();
50 (new JFrame("主窗口")).setVisible(true);
51 }
52 else
53 {
54 System.out.println("Error");
55 JOptionPane.showConfirmDialog(null,"密码错误","提示", JOptionPane.DEFAULT_OPTION);
56 }
57 }
58 }
59 class BtnAction2 implements ActionListener //内部类
60 {
61 public void actionPerformed(ActionEvent e)
62 {
63 Object o = e.getSource();
64 JButton b = (JButton)o; //强制类型转换
65 System.out.println("芝麻开门" + btn2.getText()); //类的成员/函数,可以在内部类中使用
66
67 if(b == btn2) //b.getSource == "注册"
68 {
69 jtf1.setText("");
70 jpf1.setText("");
71 }
72 }
73 }
74 public void display()
75 {
76 //JFrame frm = new JFrame(); //窗体
77 ImageIcon img1 = new ImageIcon("timg.gif"); //背景
78 JLabel background1 = new JLabel(img1);
79 this.getLayeredPane().add(background1, new Integer(Integer.MIN_VALUE));
80 //background.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
81 background1.setBounds(0, 0, 425, 450);
82 ImageIcon img2 = new ImageIcon("33.gif");
83 JLabel background2 = new JLabel(img2);
84 this.getLayeredPane().add(background2, new Integer(Integer.MIN_VALUE));
85 background2.setBounds(0, 0, img2.getIconWidth(), img2.getIconHeight());
86
87 jtf1 = new JTextField(30); //文本
88 //jtf1.setColumns(10);
89 jtf1.setSize(200,35);
90 jtf1.setLocation(130,130);
91 jpf1 = new JPasswordField(30); //密码文本
92 //jpf1.setEchoChar('#'); 设置密码文本内容
93 jpf1.setSize(200,35);
94 jpf1.setLocation(130,180);
95
96 JLabel lab1 = new JLabel("账号:"); //标题
97 Font fnt = new Font("Serief",Font.ITALIC+Font.BOLD,15);
98 lab1.setFont(fnt);
99 lab1.setBackground(Color.BLUE); //label的背景颜色
100 lab1.setOpaque(true); //label是否透明
101 lab1.setForeground(Color.YELLOW); //label前景色
102 JLabel lab2 = new JLabel("密码:");
103 lab1.setSize(50,30);
104 lab2.setSize(50,30);
105 lab1.setLocation(70,130);
106 lab2.setLocation(70,180);
107 btn1 = new JButton("登录");
108 btn1.setBounds(100,230,180,50);
109 Image im1 = (new ImageIcon("QQ.png")).getImage(); //图标
110 this.setIconImage(im1);
111 //ImageIcon im2 = new ImageIcon("77.png"); //图标
112 ImageIcon im2 = new ImageIcon("./77.png");
113 btn1.setIcon(im2);
114 this.setLayout(null); //布局--绝对布局
115 MyListener ltn = new MyListener(); //监听
116 this.addWindowListener(ltn);
117 BtnAction act = new BtnAction(); //btn事件
118 btn1.addActionListener(act);
119 btn2 = new JButton("重置");
120 btn2.setBounds(300,230,100,50);
121 BtnAction2 act2 = new BtnAction2();
122 btn2.addActionListener(act2);
123 //frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124 this.setSize(425,325);
125 this.setLocation(700,500);
126 this.setTitle("QQ登录");
127 this.setResizable(false);
128 this.add(btn1);
129 this.add(btn2);
130 this.add(lab1);
131 this.add(lab2);
132 this.add(jpf1);
133 this.add(jtf1);
134 this.add(background1);
135 this.add(background2);
136 this.setVisible(true);
137 System.out.println("OK");
138 }
139 public static void main(String[] args)
140 {
141 QQ Event1 = new QQ();
142 System.out.println("OK");
143 }
144 }