Java 整数的JTextfield侦听器
我很难找到解决方案,为Java 整数的JTextfield侦听器,java,swing,jtextfield,documentlistener,Java,Swing,Jtextfield,Documentlistener,我很难找到解决方案,为JTextField编写侦听器,特别是只允许整数值(不允许Strings)。我已经在文档监听器上试过了,但我不知道调用什么方法等等 我以前从未使用过这种类型的监听器,所以有人能解释一下我如何在JTextField上编写一个监听器,只允许接受整数值吗 基本上,在我点击一个JButton之后,在将数据提取到变量之前,监听器将不允许对其进行处理,直到输入一个整数 非常感谢。您不需要文档侦听器。在提交/确定按钮上需要一个ActionListener 确保使用JTextField的句
JTextField
编写侦听器,特别是只允许整数值(不允许String
s)。我已经在文档监听器上试过了,但我不知道调用什么方法等等
我以前从未使用过这种类型的监听器,所以有人能解释一下我如何在JTextField
上编写一个监听器,只允许接受整数值吗
基本上,在我点击一个JButton
之后,在将数据提取到变量之前,监听器将不允许对其进行处理,直到输入一个整数
非常感谢。您不需要文档侦听器。在提交/确定按钮上需要一个ActionListener 确保使用JTextField的句柄创建侦听器,然后将此代码放入
actionPerformed
调用:
int numberInField;
try {
numberInField = Integer.parseInt(myTextField.getText());
} catch (NumberFormatException ex) {
//maybe display an error message;
JOptionPane.showMessageDialog(null, "Bad Input", "Field 'whatever' requires an integer value", JOptionPane.ERROR_MESSAGE);
return;
}
// you have a proper integer, insert code for what you want to do with it here
您不需要文档侦听器。在提交/确定按钮上需要一个ActionListener 确保使用JTextField的句柄创建侦听器,然后将此代码放入
actionPerformed
调用:
int numberInField;
try {
numberInField = Integer.parseInt(myTextField.getText());
} catch (NumberFormatException ex) {
//maybe display an error message;
JOptionPane.showMessageDialog(null, "Bad Input", "Field 'whatever' requires an integer value", JOptionPane.ERROR_MESSAGE);
return;
}
// you have a proper integer, insert code for what you want to do with it here
您不需要侦听器,您需要从
JTextField
获取文本,并测试它是否为int
if (!input.getText().trim().equals(""))
{
try
{
Integer.parseInt(myString);
System.out.println("An integer"):
}
catch (NumberFormatException)
{
// Not an integer, print to console:
System.out.println("This is not an integer, please only input an integer.");
// If you want a pop-up instead:
JOptionPane.showMessageDialog(frame, "Invalid input. Enter an integer.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
您也可以使用正则表达式(有点过分,但它可以工作):
您不需要侦听器,您需要从
JTextField
获取文本,并测试它是否为int
if (!input.getText().trim().equals(""))
{
try
{
Integer.parseInt(myString);
System.out.println("An integer"):
}
catch (NumberFormatException)
{
// Not an integer, print to console:
System.out.println("This is not an integer, please only input an integer.");
// If you want a pop-up instead:
JOptionPane.showMessageDialog(frame, "Invalid input. Enter an integer.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
您也可以使用正则表达式(有点过分,但它可以工作):
如何在JTextField上编写一个侦听器,只允许接受整数值
你应该使用a或a
如何在JTextField上编写一个侦听器,只允许接受整数值
您应该使用或。JFormattedTextField示例:
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
JFormattedTextField field = new JFormattedTextField(formatter);
JOptionPane.showMessageDialog(null, field);
}
JFormattedTextField
非常适合限制输入。除了将输入限制为数字外,它还可以进行更高级的使用,例如电话号码格式。这提供了即时验证,而无需等待表单提交或类似事件。JFormattedTextField示例:
public static void main(String[] args) {
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false);
NumberFormatter formatter = new NumberFormatter(format);
formatter.setValueClass(Integer.class);
formatter.setMinimum(0);
formatter.setMaximum(Integer.MAX_VALUE);
JFormattedTextField field = new JFormattedTextField(formatter);
JOptionPane.showMessageDialog(null, field);
}
JFormattedTextField
非常适合限制输入。除了将输入限制为数字外,它还可以进行更高级的使用,例如电话号码格式。这提供了即时验证,无需等待表单提交或类似事件。OK谢谢。实际上我一直在使用parseInt。但是有没有一种方法,如果你试图输入一个字符串,那么会弹出一个某种类型的tooktip,并说“只需要int(JTextField不允许输入)”?或者这太复杂了,无法用java编写?再次感谢您可以在上面的catch子句中弹出一条错误消息,或者在对话框中放置一个JLabel以显示错误消息。哪个看起来更好取决于你还有什么,但一般来说弹出窗口是破坏性的。[编辑以包含错误对话框]好的,谢谢您的帮助。对话框是最好的解决方案。再次感谢您希望使用JFormattedTextField或DocumentFilter,以便只能输入有效数字。然后你甚至不需要做编辑,除非你有一个国王的范围条件。对于不使用JDialog的错误消息,可以使用JOptionPane。@camickr我认为您将此注释放在了错误的位置。我已经在我的答案中使用了JOptionPane来创建错误对话框。好的,谢谢。实际上我一直在使用parseInt。但是有没有一种方法,如果你试图输入一个字符串,那么会弹出一个某种类型的tooktip,并说“只需要int(JTextField不允许输入)”?或者这太复杂了,无法用java编写?再次感谢您可以在上面的catch子句中弹出一条错误消息,或者在对话框中放置一个JLabel以显示错误消息。哪个看起来更好取决于你还有什么,但一般来说弹出窗口是破坏性的。[编辑以包含错误对话框]好的,谢谢您的帮助。对话框是最好的解决方案。再次感谢您希望使用JFormattedTextField或DocumentFilter,以便只能输入有效数字。然后你甚至不需要做编辑,除非你有一个国王的范围条件。对于不使用JDialog的错误消息,可以使用JOptionPane。@camickr我认为您将此注释放在了错误的位置。我已经在回答中使用了一个JOptionPane来创建错误对话框。@DouglasGrealis您应该在您的捕获中添加类似的内容。我将更新我的示例。好的,谢谢。我可以在catch子句中使用对话框。谢谢again@DouglasGrealis你可以在你的捕获中放入类似的内容。我将更新我的示例。好的,谢谢。我可以在catch子句中使用对话框。再次感谢