final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);//多行文本框
text.setBounds(25, 25, 235, 40);
text.setText("Text");
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent event) {
String str = Character.toString(event.character);//这里不能用event.keyCode
//利用正则表达式屏蔽非法字符
event.doit = str.matches("^[0-9a-fA-F\b\r]$");//只取16进制数和退格及回车键
}
});
也可用一条代替:
event.doit = Character.toString(event.character).matches("^[0-9a-fA-F\b\r]$");
文章评论(0条评论)
登录后参与讨论