import java.awt.event.*;
import java.awt.*;
public  class TestKeyAdapter {
public static void main(String[] args) {
new MyFrame("xiaopi");
}
}
class MyFrame extends Frame{
MyFrame (String s){
super(s);
Button b = new Button("start");
TextField tf = new TextField(10);
 
add(tf,"South");
add(b,"North");
pack();
setVisible(true);

b.addActionListen (new ActionListener() {
public void actionPerformed(ActionEvent e){
TextField tf = (TextField)e.getSource();
tf.setText(e.getActionCommand());
}
});
}
}帮我看看那个错误该怎么改.

解决方案 »

  1.   

    import java.awt.event.*;
    import java.awt.*;
    public  class TestKeyAdapter {
    public static void main(String[] args) {
    new MyFrame("xiaopi");
    }
    }
    class MyFrame extends Frame{
    MyFrame (String s){
    super(s);
    Button b = new Button("start");
    final TextField tf = new TextField(10);add(tf,"South");
    add(b,"North");
    pack();
    setVisible(true);b.addActionListener (new ActionListener() {
    public void actionPerformed(ActionEvent e){
    //TextField tf = (TextField)e.getSource();
    tf.setText(e.getActionCommand());
    }
    });
    }
    }
      

  2.   

    应该是e.getSource()是一个Button,不能转型做TextField吧
      

  3.   

    最好先申明一下就可以直接调用tf了...//main函数前
    private TextField tf;
      

  4.   

    你给一个Button添加的事件,怎么可以转换成TextField呢?
      

  5.   

    TextField tf = (TextField)e.getSource();好像这一句不管有没有都没有关系呀!焦点都会到tf去!