只允许在 JTextField 中输入数字和符号 (-)
我正在尝试创建一个数学测验,我只希望用户能够输入数字,无论它们是负数还是正数。有什么办法可以做到吗?我曾考虑过使用正则表达式,但听说它们不好用。我尝试使用按键侦听器,但是如果用户粘贴怎么办?我尝试解析字符串以获取整数,但负号将不起作用。
有什么想法吗?
package com.quiz.ui;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SSCCE {
private JFrame frame;
private JPanel contentPane;
private JTextField usernameField;
public static void main(String[] arguments) {
new SSCCE().construct();
}
public void construct() {
frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(getContentPane());
frame.pack();
frame.setVisible(true);
}
public JPanel getContentPane() {
usernameField = new JTextField(5);
usernameField.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent arg0) {
int keyCode = arg0.getKeyCode();
if ((keyCode > 47 && keyCode < 58) || keyCode == 45) {
arg0.consume();
}
System.out.println(arg0.getKeyCode());
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
contentPane = new JPanel(new BorderLayout());
contentPane.add(usernameField);
return contentPane;
}
}
I'm trying to create a math quiz and I only want the user to be able to enter numbers whether they're negative or positive. Is there any way to do so? I've thought of using Regular Expressions but I've heard that they are bad to use. I tried using a keylistener but then what if the user pastes? I've tried parsing the string to get an integer but then the negative symbol will not work.
Any ideas?
package com.quiz.ui;
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class SSCCE {
private JFrame frame;
private JPanel contentPane;
private JTextField usernameField;
public static void main(String[] arguments) {
new SSCCE().construct();
}
public void construct() {
frame = new JFrame("Login");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(getContentPane());
frame.pack();
frame.setVisible(true);
}
public JPanel getContentPane() {
usernameField = new JTextField(5);
usernameField.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent arg0) {
int keyCode = arg0.getKeyCode();
if ((keyCode > 47 && keyCode < 58) || keyCode == 45) {
arg0.consume();
}
System.out.println(arg0.getKeyCode());
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
});
contentPane = new JPanel(new BorderLayout());
contentPane.add(usernameField);
return contentPane;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 DocumentFilter:
NumberOnlyFilter .java:
然后你可以像这样使用它:
Use DocumentFilter:
NumberOnlyFilter.java:
and then you can use it like:
或者,考虑验证输入 使用
InputVerifier
。 格式化文本字段也可能适用。Alternatively, consider Validating Input using an
InputVerifier
. A Formatted Text Field may also be suitable.您可以使用 Integer.parseInt(String s, int radix),对于十进制,使用 10 作为 redix 值。
You can use Integer.parseInt(String s, int radix), for decimal use 10 as a redix value.