我如何创建一个javafx对话框来更新值?

问题描述 投票:-1回答:1

我想在javafx中创建一个对话框,并在每次定义的变量的值小于1时允许它弹出,并在dialogbx文本字段的帮助下,接受大于1的新值并更新该值。我如何无法使它正常工作以及任何帮助将不胜感激。

TextInputDialog dialog = new TextInputDialog("Enter value");
dialog.setTitle("Text Input Dialog");
dialog.setHeaderText("Look, a Text Input Dialog");
dialog.setContentText("Please enter value:");

Optional<String> result = dialog.showAndWait();
if (result.isPresent()){
     Apple.value= Apple.value+result.get());
}
java javafx dialog
1个回答
0
投票

当使用标准TextInputDialog时,您会得到一个TextField,因此您需要检查用户输入是否为有效的可解析数字。这是一个带有IntegerProperty的小例子:

控制器类:

package sample;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextInputDialog;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;


public class Controller implements Initializable {

    @FXML
    private Label currentValueLabel;

    private IntegerProperty value = new SimpleIntegerProperty(1);


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        // Label to show the current value:
        currentValueLabel.textProperty().bind(value.asString("Current value: %d"));

        // Add a listener to the value property:
        value.addListener((observable, oldValue, newValue) -> {
            // Show dialog as long as a valid input was made:
            while (value.get() < 1) {
                TextInputDialog dialog = new TextInputDialog("1");
                dialog.setTitle("Text Input Dialog");
                dialog.setHeaderText("Look, a Text Input Dialog");
                dialog.setContentText("Please enter value:");

                Optional<String> result = dialog.showAndWait();

                if (result.isPresent()) {
                    int input;
                    try {
                        // Parse the user input:
                        input = Integer.parseInt(result.get());

                        if (input < 1) {
                            // User entered an integer value but less than 1:
                            System.err.println("Invalid value: " + input);
                        } else {
                            // User entered a valid value:
                            this.value.set(input);
                        }
                    } catch (NumberFormatException e) {
                        // Could not parse user input:
                        System.err.println("Parse error " + e.getMessage());
                    }
                }
            }
        });
    }

    @FXML
    public void handleChangeValueBtnClick() {
        // Change the value to zero which triggers the listener:
        value.set(0);
    }
}

FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>


<VBox xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Label fx:id="currentValueLabel" text="Current value: " />
      <Button mnemonicParsing="false" onAction="#handleChangeValueBtnClick" text="Change value to zero" />
   </children>
</VBox>

您还可以使用带有定义的最小值的Spinner控件创建自定义对话框,以便用户只能输入有效值。

© www.soinside.com 2019 - 2024. All rights reserved.