日期:2014-05-20  浏览次数:20798 次

多事件处理怎么只有一个显示输入字符
如下一个多事件处理的程序,为什么我运行后Label   action没有输入的字符,只有
Label   text中有显示。
import   java.awt.*;
import   java.awt.event.*;
public   class   MultiEvent1   extends   Frame   implements   ActionListener,TextListener{
Label   action=new   Label();
Label   text=new   Label();
TextField   tf=new   TextField(20);
public   MultiEvent1(String   title){
super(title);
action.setBackground(Color.GREEN);
text.setBackground(Color.PINK);
add(action, "North ");
add(text, "Center ");
add(tf, "South ");
tf.addActionListener(this);
tf.addTextListener(this);
}
public   void   actionPerformed(ActionEvent   e){
action.setText(tf.getText());
}
public   void   textValueChanged(TextEvent   e){
text.setText(tf.getText());
}
public   static   void   main(String[]   args){
Frame   f=new   MultiEvent1( "多事件处理 ");
f.pack();
f.setVisible(true);
}
}

------解决方案--------------------
TextField的Action事件MS在按下回车的时候才触发。
------解决方案--------------------
没触发actionPerformed 当然没执行action.setText(tf.getText());

把ActionListener换成KeyListener
重写keyTyped或者KeyReleased方法。
可以得到你想要的结果。