JApplet - 不允许使用字母表
这是我的代码,写在我的小程序中
KeyListener keyListener = new KeyListener()
{
public void keyPressed(KeyEvent keyEvent)
{
validate valid=new validate();
valid.errorMessage(txt_district_id, keyEvent);
}
public void keyReleased(KeyEvent keyEvent)
{
}
public void keyTyped(KeyEvent keyEvent)
{
}
};
txt_district_id.addKeyListener(keyListener);
,验证类的代码
public class validate
{
public String errorMessage(KeyEvent keyEvent,JTextField txt)
{
int keyCode = keyEvent.getKeyCode();
String keyText = KeyEvent.getKeyText(keyCode);
//msg.setText(title + " : " + keyText + " / " + keyEvent.getKeyChar());
if(keyCode > 47 && keyCode < 58)
{
txt.setEditable(true);
}
else
{
txt.setEditable(false);
return "Only Numeric Value Accepted";
}
}
}
一切正常,但问题是每当用户输入任何字母时,文本字段就会被禁用,这就是我的问题。我的意思是,无论如何都不能输入字母表,并且应该启用文本字段。提前致谢。!!
this is my code, which is written inside my applet
KeyListener keyListener = new KeyListener()
{
public void keyPressed(KeyEvent keyEvent)
{
validate valid=new validate();
valid.errorMessage(txt_district_id, keyEvent);
}
public void keyReleased(KeyEvent keyEvent)
{
}
public void keyTyped(KeyEvent keyEvent)
{
}
};
txt_district_id.addKeyListener(keyListener);
and code of validate class is
public class validate
{
public String errorMessage(KeyEvent keyEvent,JTextField txt)
{
int keyCode = keyEvent.getKeyCode();
String keyText = KeyEvent.getKeyText(keyCode);
//msg.setText(title + " : " + keyText + " / " + keyEvent.getKeyChar());
if(keyCode > 47 && keyCode < 58)
{
txt.setEditable(true);
}
else
{
txt.setEditable(false);
return "Only Numeric Value Accepted";
}
}
}
everything working properly, but the problem is whenever user input any alphabet the textfield will become disable, and that is my problem. I mean it should like, alphabet can not be entered and textfield should be enabled in any case. Thanks in advance.!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Listener
是为史前指定的AWT 组件,用于 Swing JComponents (JApplet) 使用 按键绑定Listener
is designated for prehistoric AWT Components, for Swing JComponents (JApplet) use KeyBindings