java 如何设置jTextField文本框只能输入数字?
2个回答
展开全部
加上以下KeyListener监听事件就可以了。
public class VoteElectKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
int keyChar=e.getKeyChar();
if (keyChar>=KeyEvent.VK_0 && keyChar<=KeyEvent.VK_9) {
} else {
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
public class VoteElectKeyListener implements KeyListener {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
int keyChar=e.getKeyChar();
if (keyChar>=KeyEvent.VK_0 && keyChar<=KeyEvent.VK_9) {
} else {
e.consume();
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询