Java 来自文本字段的整数

Java int from textfield

我是 Java 的新手。我正在尝试从 JavaFX 中的计算器的文本字段中获取一个整数。我有 2 个文本字段,人们可以在其中输入数字:

hb.getChildren().addAll(label1, textField);
textField.getText();
hb.setSpacing(10);
hb.getChildren().addAll(label2, textField2);
textField2.getText();
hb.setSpacing(10);

这就是我从文本字段中获取 int 的方法,但它不起作用:

int x = Integer.parseInt(textField.getText());
int y = Integer.parseInt(textField2.getText());

有谁知道我做错了什么?我在互联网上看到人们使用事件,但我不确定在这种情况下我是否必须使用它。

错误日志:

 Exception in Application start method
 java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
 Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication4(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
 Caused by: java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at test.start(test.java:57)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication11(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait4(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null2(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater3(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null7(WinApplication.java:177)
    ... 1 more Exception running application test

希望有人能帮助我,

托马斯

先生,问题是您尝试将空字符串转换为整数,如错误所示:

Caused by: java.lang.NumberFormatException: For input string: ""

为了防止这种情况,您可以将其添加到您的代码中

int x,y;
try{
     x = Integer.parseInt(textField.getText().trim());
} catch(NumberFormatException ex) {
    x = 0;
}
try{
     y = Integer.parseInt(textField2.getText().trim());
} catch(NumberFormatException ex) {
    y = 0;
}

I saw on internet people used event but I'm not sure if in this case I have to use it.

TextField 默认包含空字符串。如果您不使用某种事件或 text 属性 的侦听器,您将不会获得用户可以在该字段中输入的值。

在这种情况下,我建议使用 TextFormatter 来简化事情:

TextField tf1 = new TextField();
TextField tf2 = new TextField();

StringConverter<Integer> converter = new IntegerStringConverter();

TextFormatter<Integer> formatter1 = new TextFormatter<>(converter, 0);
TextFormatter<Integer> formatter2 = new TextFormatter<>(converter, 0);

tf1.setTextFormatter(formatter1);
tf2.setTextFormatter(formatter2);

Label sum = new Label();
sum.textProperty().bind(IntegerExpression.integerExpression(formatter1.valueProperty())
        .add(IntegerExpression.integerExpression(formatter2.valueProperty())).asString("Sum: %d"));

VBox root = new VBox(tf1, tf2, sum);

您可以简单地使用

int x = formatter1.getValue();
int y = formatter1.getValue();

如果您不想要绑定。格式化程序防止用户在字段中留下无效数据,并在用户离开字段或按回车键时负责更新它的 value 属性。