top

一個AWT事件處理的習題..求解

AWT按鍵事件的處理--KeyEvent類別
1.用亂數取兩個最多2位數的整數,由使用者計算並輸入這兩個整數的和,
由程式判斷是否正確。

目前小弟卡在如何取得輸入的答案來進行判斷是否正確。
在KeyType函數裡使用getKeyChar()取得的似乎是字元碼。
我該如何取得輸入,還請前輩指導。

這是目前小弟寫出的東西


import java.awt.*;
import java.awt.event.*;

public class Nana extends Frame implements ActionListener,KeyListener{
  static Nana frm = new Nana();
  static Label lb1 = new Label();
  static Label lb2 = new Label("87");
  static Label lb3 = new Label();
  static int sum;
  static TextField tf = new TextField();
  static Button btn = new Button("Next");
  static int random1,random2;
  public static void main(String args[]){
    random1=(int)(Math.random()*100);
    random2=(int)(Math.random()*100);
    frm.setLayout(new FlowLayout(FlowLayout.CENTER));
    frm.setSize(200,150);
    btn.addActionListener(frm);
    lb1.setText(random1+"+"+random2+"=");
    frm.add(btn);
    frm.add(lb1);
    frm.add(lb2);
    frm.setTitle("Layout");
    frm.add(lb3);
    frm.add(tf);
    frm.add(btn);
    tf.addKeyListener(frm);
    frm.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});
    frm.setVisible(true);
  
  }
  public void actionPerformed(ActionEvent e){
    random1=(int)(Math.random()*100);
    random2=(int)(Math.random()*100);
    lb1.setText(random1+"+"+random2+"=");
   
   
    if(sum==(random1+random2)){
      lb2.setText("正確");
    }
    else
      lb2.setText("錯誤");
  }
  
  public void keyTyped(KeyEvent e) {
    sum=e.getKeyChar();
    System.out.println(sum=>KeyEvent.VK_0 && keyChar<=KeyEvent.VK_9);
   
  }
  
  public void keyPressed(KeyEvent e) {
   
   
  }

  public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
   
  }
}