簡體   English   中英

來自文本字段的Java int

[英]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$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

我希望有一個人可以幫助我,

托馬斯

我在互聯網上看到有人使用事件,但不確定在這種情況下是否必須使用它。

默認情況下, 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();

如果您不想綁定。 格式化程序可防止用戶在字段中保留無效數據,並在用戶離開字段或按Enter時負責更新其value屬性。

先生,問題是您嘗試將空字符串轉換為整數,錯誤顯示:

原因:java.lang.NumberFormatException:對於輸入字符串:“”

為了防止這種情況,您可以將此添加到您的代碼中

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM