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

java窗体中的textbox事件
为什么运行后我在text1里输入sun,text3里不会出现太阳?

import java.awt.*;
import java.awt.event.*;
public class Window extends Frame implements ActionListener{

TextField text1,text2,text3;
Window(String s){
super(s);
text1=new TextField(8);
text1.setSize(50, 20);
text2=new TextField(8);
text2.setSize(50, 20);
text3=new TextField(15);
  text3.setSize(50,20);
text1.addActionListener(this);
text2.addActionListener(this);
add(text1);
add(text2);
add(text3);

setSize(500,450);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==text1){
String word=text1.getText();
if(word.equals("boy")){
text3.setText("男孩儿");
}else if(word.equals("girl")){
text3.setText("女孩儿");
}else if(word.equals("sun")){
text3.setText("太阳");
}else{
text3.setText("没有该单词");
}
}else if(e.getSource()==text2){
String word=text2.getText();
if(word.equals("男孩儿")){
text3.setText("boy");
}else if(word.equals("女孩儿")){
text3.setText("girl");
}else if(word.equals("太阳")){
text3.setText("sun");
}else{
text3.setText("没有该单词");
}
}

}

}

public class Text {
public static void main(String[] args){
Window window=new Window("java");
}
}

------解决方案--------------------
按下回车键才会激发ActionEvent。
你可以使用 java.awt.event.TextListener