1 public static void main(String[] args) { 2 3 Frame f = new Frame(); 4 5 //關閉窗體 6 f.addWindowListener(new WindowAdapter() { 7 @Override 8 public void windowClosing(WindowEvent e) { 9 System.exit(0); 10 } 11 }); 12 13 f.setTitle("QQ登錄");//添加標題 14 f.setSize(420, 230);//設置窗體的尺寸 15 f.setLocation(455, 207);//設置窗體出現坐標 16 f.setLayout(null);//清除窗體默認布局 17 f.setIconImage(Toolkit.getDefaultToolkit().getImage("F:\\qq.png"));//設置圖標 18 f.setResizable(false);//禁止窗體改變尺寸 19 20 21 22 //賬號標簽 23 Label user = new Label("賬號:"); 24 user.setLocation(75, 50); 25 user.setSize(50, 25); 26 user.setFont(new Font("微軟雅黑",Font.BOLD,16)); 27 f.add(user); 28 29 //密碼標簽 30 Label password = new Label("密碼:"); 31 password.setLocation(75, 100); 32 password.setSize(50, 25); 33 password.setFont(new Font("微軟雅黑",Font.BOLD,16)); 34 f.add(password); 35 36 //輸入賬號的文本框 37 TextField t1 = new TextField(); 38 //鍵盤輸入監聽 39 t1.addKeyListener(new KeyAdapter() { 40 @Override 41 public void keyTyped(KeyEvent e) { 42 int key = e.getKeyChar(); 43 if(key>=KeyEvent.VK_0 && key<=KeyEvent.VK_9){ 44 45 }else{ 46 e.consume(); 47 } 48 } 49 }); 50 t1.setSize(220,25); 51 t1.setLocation(130, 50); 52 t1.setFont(new Font("微軟雅黑",Font.PLAIN,16)); 53 f.add(t1); 54 55 //輸入密碼的文本框 56 TextField t2 = new TextField(); 57 t2.setEchoChar('*'); 58 t2.setSize(220,25); 59 t2.setLocation(130, 100); 60 t2.setFont(new Font("微軟雅黑",Font.PLAIN,16)); 61 f.add(t2); 62 63 //登錄按鈕 64 Button login = new Button("登錄"); 65 //按鈕觸發事件 66 login.addActionListener(new ActionListener() { 67 68 @Override 69 public void actionPerformed(ActionEvent arg0) { 70 String zh = t1.getText(); 71 String ma = t2.getText(); 72 if(zh.equals("34598700") && ma.equals("meinv123")){ 73 System.out.println("登錄成功"); 74 }else{ 75 JOptionPane.showMessageDialog(f, "賬號或密碼輸入錯誤"); 76 t1.setText(""); 77 t2.setText(""); 78 } 79 } 80 }); 81 login.setLocation(100, 160);//按鈕在窗體中的坐標 82 login.setSize(75, 30);//設計按鈕的尺寸 83 f.add(login);//把按鈕元素添加到窗體中 84 85 //注冊按鈕 86 Button reg = new Button("注冊"); 87 //觸發事件 88 reg.addActionListener(new ActionListener() { 89 90 @Override 91 public void actionPerformed(ActionEvent e) { 92 new Reg(); 93 } 94 }); 95 reg.setLocation(250, 160);//按鈕在窗體中的坐標 96 reg.setSize(75, 30);//設計按鈕的尺寸 97 f.add(reg);//把按鈕元素添加到窗體中 98 99 f.setVisible(true);//設置窗體的可見性 100 101 }
