標籤:錯誤 public efault res listen 使用 介面 resizable print
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 }
使用Java的Frame類編寫的QQ登入介面