求高手指点事件的相关问题
我设置了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());
}
}
}
----------------解决方案--------------------------------------------------------
你这个没错啊!要是像你说的在 2 里输完了就传到 3 ,可是你要想,计算机它怎么知道你输完了没有啊,所以做完输入还是要你自己退出 2 或者说回车的它才把结果传到 3 了。要是想要你说的那样输完了,让它自己完成+法也是可以的,就是你设置一个时间,比如说过了3秒,你就让它自动+了,可是这样不是更不好用了吗!
----------------解决方案--------------------------------------------------------
你的很多Listener没什么用处啊! 我用你的代码加了一个TextListener可以实现你说的功能。
程序代码:
package testText;
import java.awt.*;
import java.awt.event.*;
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.setFocusable(false);
Label label1 = new Label("+");
Label label2 = new Label("=");
setLayout(new FlowLayout(FlowLayout.LEFT));
setLocation(300,400);
setSize(400,300);
setBackground(Color.cyan);
text1.addFocusListener(new CounterFocusListener());
text2.addFocusListener(new CounterFocusListener());
// text3.addFocusListener(new CounterFocusListener());
text3.addTextListener(new textLister());
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 textLister implements TextListener {
@Override
public void textValueChanged(TextEvent e) {
text3.setFocusable(true);
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
text3.setText(""+ (num1 + num2));
}
}
}
import java.awt.*;
import java.awt.event.*;
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.setFocusable(false);
Label label1 = new Label("+");
Label label2 = new Label("=");
setLayout(new FlowLayout(FlowLayout.LEFT));
setLocation(300,400);
setSize(400,300);
setBackground(Color.cyan);
text1.addFocusListener(new CounterFocusListener());
text2.addFocusListener(new CounterFocusListener());
// text3.addFocusListener(new CounterFocusListener());
text3.addTextListener(new textLister());
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 textLister implements TextListener {
@Override
public void textValueChanged(TextEvent e) {
text3.setFocusable(true);
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
text3.setText(""+ (num1 + num2));
}
}
}
----------------解决方案--------------------------------------------------------
回复 3楼 jackieli2074
你好,请问下为什么我把你的代码加进去就是错误的呢,他说方法未覆盖其父类的方法,就是这个@Override请问还需要改动吗?
----------------解决方案--------------------------------------------------------
回复 4楼 skylm1234
这个是应该是因为你用的JDK1.5吧!要么该1.6要么把那一句 @Override删除----------------解决方案--------------------------------------------------------