【问题标题】:Converting decimal input in a Javafx textfield to a double将 Javafx 文本字段中的十进制输入转换为双精度
【发布时间】:2018-09-13 15:03:57
【问题描述】:

所以,我是 javafx 的新手,我试图使用文本字段来接收一个字符串,然后,如果可能的话,使用 try catch 将字符串转换为双精度。我遇到的唯一问题是,如果我输入一个小数,例如 1000.56,catch 就会激活,并且我的错误标签会弹出,说它不能接受字符串。这是相关的代码块,请假设我已经完成了所有正确的导入和变量的所有基本设置。

            //takes in the users input and trims spaces
            holder[i] = txtFld.getText().trim();
            while(run == false) {
                try {
                    //attempts to parse the stripped text to a double
                    amt[i] = Double.parseDouble(holder[i]);
                    //allows the loop to break
                    run = true;
                }catch(NumberFormatException ex) {
                    txtFld.setText("");
                    //tells the user about the error
                    grid.add(Err, 0, 3);
                }
            }

【问题讨论】:

  • 您是否打印出holder[i] 的值以确保您传递给parseDouble 的内容实际上是"1000.56"
  • 天哪;我觉得非常愚蠢。我有它,所以在那个提示之后,另一个使用相同的文本字段出现了,我不小心把清除它的东西放在了开头。

标签: java javafx


【解决方案1】:

出于以下几个原因,您不应该在 JavaFX 中执行这样的循环:

  • 这样的循环会阻塞应用程序线程。这会导致循环运行时不处理任何输入。
  • 即使输入未完成,您也可以修改文本。考虑用户想要输入-1E3 的场景。您的代码允许此输入的唯一方法是逐步更改文本,如下所示:"" -> "1" -> "-1" -> "-13" -> "-1E3"

最简单的解决方法是简单地检查“提交”数据。或者听TextField.text 属性并显示一些无效输入的指示(例如一些图标),但不要在每次更改时修改文本

已经有一些实现可以尝试解析失去焦点的文本:TextFormatter:

@Override
public void start(Stage primaryStage) {
    TextField textField = new TextField();

    TextFormatter<Double> formatter = new TextFormatter<>(new DoubleStringConverter(), 0d);
    textField.setTextFormatter(formatter);

    formatter.valueProperty().addListener((o, oldValue, newValue) -> System.out.println("value changed to " + newValue));

    Button button = new Button("some other focusable element");

    Scene scene = new Scene(new VBox(textField, button));
    primaryStage.setScene(scene);
    primaryStage.show();
}

编辑

对于“提交”按钮,只需验证来自事件处理程序的值:

@Override
public void start(Stage primaryStage) {
    TextField textField = new TextField();
    Label textErrorLabel = new Label();
    textErrorLabel.setTextFill(Color.RED);

    HBox textBox = new HBox(10, textField, textErrorLabel);
    textBox.setPrefWidth(300);

    Button button = new Button("Submit");
    button.setOnAction(evt -> {
        boolean valid = true;
        double value = 0;
        try {
            value = Double.parseDouble(textField.getText());
            textErrorLabel.setText("");
            textField.setStyle(null);
        } catch (NumberFormatException ex) {
            valid = false;
            textErrorLabel.setText("erroneous input");
            textField.setStyle("-fx-control-inner-background: red;");
        }

        // you could do more input validation here...

        if (valid) {
            System.out.println("successfully submitted "+ value);
        }
    });

    Scene scene = new Scene(new VBox(textBox, button));
    primaryStage.setScene(scene);
    primaryStage.show();
}

【讨论】:

  • 感谢您的信息,但我有基于用户按下侧面单独按钮的 actionEvent;我想我应该提到这一点,抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 2014-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-31
相关资源
最近更新 更多