gpt4 book ai didi

Java int 从文本字段

转载 作者:行者123 更新时间:2023-12-02 11:20:45 25 4
gpt4 key购买 nike

我是 Java 新手。我正在尝试从 JavaFX 计算器的文本字段中获取 int 。我有 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$launchApplication$154(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$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$null$147(WinApplication.java:177)
... 1 more Exception running application test

希望有人能帮助我

托马斯

最佳答案

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();

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

关于Java int 从文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49937561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com