求高手指点事件的相关问题
我设置了3个TextField,,目的是在TextField1输入的数显示在TextField3中,输入一个它就自动显示一个饿,输2个它显示2个,就是这样的,请问怎么处理呀,我先是监听的TextField1,但如果把这些数转到TextField3中呀。。谢谢了,可能问题问得不是很完整。。要求是,在TextField1中输入数字,在TextField3中显示你输入的数字,当在TextField2中输入数字时,TextField3中自动显示TextField1和TextField2的和,也就是做加法运算,拜托各位了!!!
但是我始终做不到让它同步。。初学JAVA,还望高手指点呀。。
下面是我的源代码,请问应该怎么修改才能达到效果呢,谢谢!代码有点杂乱。。。
import java.awt.*;
import java.awt.event.*;
public class TextCounter {
public static void main (String args[]) {
new CounterFrame().lauchFrame();
}
}
class CounterFrame extends Frame {
TextField text1,text2,text3;
public void lauchFrame() {
text1 =new TextField ("0",10);
text2 =new TextField ("0",10);
text3 =new TextField ("0",10);
text3.setEditable(false);
Label label1 = new Label("+");
Label label2 = new Label("=");
setLayout(new FlowLayout(FlowLayout.LEFT));
setLocation(300,400);
setSize(400,300);
setBackground(Color.red);
text1.addFocusListener(new CounterFocusListener());
text2.addFocusListener(new CounterFocusListener1());
//text2.addFocusListener(new CounterFocusListener());
//text2.add(new CounterFocusListener());
text3.addKeyListener(new CounterKeyAdapter());
//setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
add(text1);
add(label1);
add(text2);
add(label2);
add(text3);
setVisible(true);
}
class CounterFocusListener implements FocusListener{
public void focusGained(FocusEvent e) {
//text3.setFocusable(true);
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
text3.setText(""+ (num1 + num2));
}
public void focusLost(FocusEvent e) {
//text3.setFocusable(true);
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
text3.setText(""+ (num1 + num2));
}
}
class CounterFocusListener1 implements FocusListener{
public void focusGained(FocusEvent e) {
//text3.setFocusable(true);
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
text3.setText(""+ (num1 + num2));
}
public void focusLost(FocusEvent e) {
//text3.setFocusable(true);
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
text3.setText(""+ (num1 + num2));
}
}
class CounterKeyAdapter extends KeyAdapter {
public void keyTyped (KeyEvent e) {
//text3.setText(text1.getText());
//text3.setFocusable(true);
text3.setText("" + e.getKeyChar());
}
}
}