当前位置: 技术问答>java相关
关于java.awt.AWTEvent中consume()的一个问题?
来源: 互联网 发布时间:2015-03-24
本文导语: ........... private class KListener extends KeyAdapter{ public void keyTyped(KeyEvent e){ e.consume(true);//1 tell me why????? System.out.println(e.Isconsume()); //2 tell me why????? ...
...........
private class KListener extends KeyAdapter{
public void keyTyped(KeyEvent e){
e.consume(true);//1 tell me why?????
System.out.println(e.Isconsume()); //2 tell me why?????
}
}
............
请问1,2为什么出错,搞不懂了,请高手指点?主题可以通过这里来实现!!!!
private class KListener extends KeyAdapter{
public void keyTyped(KeyEvent e){
e.consume(true);//1 tell me why?????
System.out.println(e.Isconsume()); //2 tell me why?????
}
}
............
请问1,2为什么出错,搞不懂了,请高手指点?主题可以通过这里来实现!!!!
|
protected void consume() {
switch(id) {
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_DRAGGED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
consumed = true;
break;
default:
// event type cannot be consumed
}
}
protected boolean isConsumed() {
return consumed;
}
switch(id) {
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
case MouseEvent.MOUSE_PRESSED:
case MouseEvent.MOUSE_RELEASED:
case MouseEvent.MOUSE_MOVED:
case MouseEvent.MOUSE_DRAGGED:
case MouseEvent.MOUSE_ENTERED:
case MouseEvent.MOUSE_EXITED:
consumed = true;
break;
default:
// event type cannot be consumed
}
}
protected boolean isConsumed() {
return consumed;
}
|
参考。
Limit TextField input to a maximum length
[JDK11] import java.awt.*;
import java.awt.event.*;
public class TextFieldWithLimit extends TextField
implements KeyListener {
private int maxLength;
public TextFieldWithLimit
(String initialStr,int col,int maxLength) {
super(initialStr,col);
this.maxLength = maxLength;
addKeyListener(this);
}
public TextFieldWithLimit (int col,int maxLength) {
this("",col,maxLength);
}
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
int len = getText().length();
if (len
Limit TextField input to a maximum length
[JDK11] import java.awt.*;
import java.awt.event.*;
public class TextFieldWithLimit extends TextField
implements KeyListener {
private int maxLength;
public TextFieldWithLimit
(String initialStr,int col,int maxLength) {
super(initialStr,col);
this.maxLength = maxLength;
addKeyListener(this);
}
public TextFieldWithLimit (int col,int maxLength) {
this("",col,maxLength);
}
public void keyPressed(KeyEvent e) {
char c = e.getKeyChar();
int len = getText().length();
if (len