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;
}
解决方案 »
- 有关多线程的问题
- 关于继承情况下打包编译不通过,请高手指点
- 急急急 javaSE程序中使用proxool都需要哪些jar文件
- sf
- 怎么用sockt发送tcp数据包?
- 问个ButtonGroup的问题
- Exception in thread "main" java.lang.NoClassDefFoundError: maths (wrong name: cm/mxq/test/maths)
- 关于多线程的简单问题,在线等待!!!!
- 关于安装JDK的小问题?
- 疾风摩郎 进来拿分啊
- help! JAVA下有类似VC++ 下的socket 类吗?请各位高手看一下!!
- 为什么JCreator Pro 2.00_007不能进行单步调试,Build菜单下单步调试的选项是灰色的???好痛苦。。
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 < maxLength) {
return;
}
else {
if((c==KeyEvent.VK_BACK_SPACE)||
(c==KeyEvent.VK_DELETE) ||
(c==KeyEvent.VK_ENTER)||
(c==KeyEvent.VK_TAB)||
e.isActionKey())
return;
else {
e.consume();
}
}
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
好象运行e.consume()后,consume由false 变为true,再运行一下e.consume(),那个consume()还是不变啊!!!给你们加分先!!