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