小白提问java中KeyEvent的小问题。
代码入下:importjava.awt.*;importjava.awt.event.*;publicclassTestKeyEvent{publicstaticvoid...
代码入下:
import java.awt.*;
import java.awt.event.*;
public class TestKeyEvent {
public static void main(String[] args) {
MyFrame f = new MyFrame("Typing Window");
TextField tf1 = new TextField();
TextField tf2 = new TextField();
tf1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
tf2.setText(((TextField)e.getSource()).getText());
}
});
f.add(tf1, BorderLayout.NORTH);
f.add(tf2, BorderLayout.SOUTH);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.pack();
}
}
class MyFrame extends Frame {
MyFrame(String s) {
super(s);
setVisible(true);
}
}
结果为什么不能同步?
在tf1中输入的内容在tf2中老是少显示一位?? 展开
import java.awt.*;
import java.awt.event.*;
public class TestKeyEvent {
public static void main(String[] args) {
MyFrame f = new MyFrame("Typing Window");
TextField tf1 = new TextField();
TextField tf2 = new TextField();
tf1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
tf2.setText(((TextField)e.getSource()).getText());
}
});
f.add(tf1, BorderLayout.NORTH);
f.add(tf2, BorderLayout.SOUTH);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.pack();
}
}
class MyFrame extends Frame {
MyFrame(String s) {
super(s);
setVisible(true);
}
}
结果为什么不能同步?
在tf1中输入的内容在tf2中老是少显示一位?? 展开
1个回答
展开全部
把你的public void keyTyped(KeyEvent e) 这个方法换成下面的这个看看
public void keyReleased(KeyEvent e)他就三个方法你自己不先一个一个试一下就问百度了。程序就是你觉得不可能也要实践的。
还有我觉得你写的代码怪怪的,老是用匿名类为什么这么懒呢,匿名类好些但不方便啊。
下面我改了一下,你如果是老板会要谁的代码?
import java.awt.*;
import java.awt.event.*;
public class TestKeyEvent extends Frame implements KeyListener {
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TestKeyEvent() {
super("Typing Window");
tf1.addKeyListener(this);
this.add(tf1, BorderLayout.NORTH);
this.add(tf2, BorderLayout.SOUTH);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.pack();
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
String t = tf1.getText();
tf2.setText(t);
}
public static void main(String[] args) {
TestKeyEvent Tk = new TestKeyEvent();
}
}
public void keyReleased(KeyEvent e)他就三个方法你自己不先一个一个试一下就问百度了。程序就是你觉得不可能也要实践的。
还有我觉得你写的代码怪怪的,老是用匿名类为什么这么懒呢,匿名类好些但不方便啊。
下面我改了一下,你如果是老板会要谁的代码?
import java.awt.*;
import java.awt.event.*;
public class TestKeyEvent extends Frame implements KeyListener {
TextField tf1 = new TextField();
TextField tf2 = new TextField();
TestKeyEvent() {
super("Typing Window");
tf1.addKeyListener(this);
this.add(tf1, BorderLayout.NORTH);
this.add(tf2, BorderLayout.SOUTH);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.pack();
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
String t = tf1.getText();
tf2.setText(t);
}
public static void main(String[] args) {
TestKeyEvent Tk = new TestKeyEvent();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询