继承JFrame,只是初步
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 12 } 13 class FixFrame extends JFrame 14 { 15 this.setResizable(false); 16 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 17 } 18 class ResizableFrame extends JFrame 19 { 20 this.setResizable(true); 21 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 } 23 class LoginFrame extends FixFrame 24 { 25 LoginFrame() //构造函数 26 { 27 display();//调用display()函数 28 } 29 private JButton btn1; 30 private JButton btn2; 31 private JTextField jtf1; 32 private JPasswordField jpf1; //密码文本 33 private ImageIcon img1; 34 private JLabel background1; 35 private ImageIcon img2; 36 private JLabel background2; 37 private JLabel lab1; 38 private Font fnt; 39 private JLabel lab2; 40 private Image im1; 41 private ImageIcon im2; 42 private MyListener ltn; 43 private BtnAction act; 44 private BtnAction2 act2; 45 class MyListener extends WindowAdapter //适配器 是扩展 不需要覆盖WindowAdapter中的所有方法 功能代码较多 46 { 47 public void windowClosing(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 void actionPerformed(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 void actionPerformed(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 void display() 101 { 102 //JFrame frm = new JFrame(); //窗体 103 img1 = new ImageIcon("timg.gif"); //背景 104 background1 = new JLabel(img1); 105 this.getLayeredPane().add(background1, new Integer(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 = new JLabel(img2); 110 this.getLayeredPane().add(background2, new Integer(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 = new BtnAction2(); 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 void main(String[] args) 166 { 167 LoginFrame Event1 = new LoginFrame(); 168 System.out.println("OK"); 169 } 170 } 171 class RegsterFrame extends FixFrame 172 { 173 174 } 175 class BackPswFrame extends FixFrame 176 { 177 178 } 179 class MainFrame extends ResizableFrame 180 { 181 182 }