是否可以从close方法返回javaFX中的变量?有没有更有效的方法可以做到这一点?

尼尔·埃斯特拉达(Neil Estrada)

我想运行当选择一个答案或窗口被手动关闭,将关闭一个弹出窗口,然后返回选择的值(布尔值)的方法。不幸的是,我不知道如何检索数据,因为我最初调用的是显示查询的方法,而不是close方法。

我尝试了很多不同的方法,但是当我尝试使用close方法时,无法检索从它返回的数据。否则,数据将在用户能够对其进行突变之前返回。均未达到我的目标

这是我创建盒子的方法。我目前只是关闭窗口,而不是通过其他关闭方法关闭。

public class ConfirmBox
{
   static boolean answer;
   public static boolean display(String title, String question) {
         Stage window = new Stage();
        //Block events to other windows
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle(title);
        window.setMinWidth(250);
        //LabelQ
        Label label = new Label();
        label.setText(question);
        //YesBox
        Button yesB= new Button("Yes");
        yesB.getStyleClass().add("button-green");
        yesB.setOnAction(e -> {
            answer=true;
            window.close();
        });
        //NoBox
        Button noB= new Button("No");
        noB.getStyleClass().add("button-red");
        yesB.setOnAction(e -> {
            answer=false;
            window.close();
        });

        window.setOnCloseRequest(e ->{
            e.consume();
            window.close();
        });

        VBox layout = new VBox(10);
        layout.getChildren().addAll(label, yesB, noB);
        layout.setAlignment(Pos.CENTER);

        //Display window and wait for it to be closed before returning
        Scene scene = new Scene(layout, 800, 200);
        scene.getStylesheets().add("styles.css");
        window.setScene(scene);
        window.showAndWait();
        return answer;
    }
}

这是我到目前为止运行的所有方法。

public class Main extends Application
{
    Stage window;
    String gender;
    String name;
    public static void main (String[] args){
        launch (args);
    }
    public void start(Stage primaryStage) throws Exception{
        window= primaryStage;
        window.setTitle("Pokemon Gray");
        GridPane grid= new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setVgap(8);
        grid.setHgap(10);
        // Name Label
        Label nameLabel= new Label("UserName");
        GridPane.setConstraints(nameLabel,0, 0);

        //Name input
        TextField nameInput = new TextField();
        GridPane.setConstraints(nameInput,1, 0);


        //GenderLabel
        Label genderLabel= new Label("Gender");
        GridPane.setConstraints(genderLabel,0, 1);

        //Gender Boy
        Button boySelect= new Button("Boy");
        boySelect.getStyleClass().add("button-blue");
        GridPane.setConstraints(boySelect, 1, 1);
        boySelect.setOnAction(e-> gender="Male");


        //Gender Girl
        Button girlSelect= new Button("Girl");
        girlSelect.getStyleClass().add("button-red");
        GridPane.setConstraints(girlSelect, 2, 1);
        girlSelect.setOnAction(e-> gender="Female");

        //Login
        Button saveName= new Button("Save Name");
        GridPane.setConstraints(saveName, 1, 2);
        saveName.setOnAction( e -> {
            boolean valid =verifyText(nameInput.getText());
            if ((valid)&&((gender == "Male")||(gender=="Female"))){
                String qStr= "Are you sure your name is "+nameInput.getText()+" and you are a "+gender+"?";
                boolean confirmed= ConfirmBox.display("Confirmation", qStr );
                if (confirmed == true){
                    name= nameInput.getText();
                }
            }
            else{
                ErrorBox.display("Error", "You must enter a name less than 20 characters long and a gender");
            }
        });

        grid.getChildren().addAll(nameLabel, nameInput, genderLabel, boySelect, girlSelect, saveName);
        Scene scene= new Scene(grid, 1000, 1000);
        scene.getStylesheets().add("styles.css");
        window.setScene(scene);
        window.show();
    }
    public boolean verifyText(String text){
        boolean valid;
        if ((text.length() > 0) && (text.length() <= 20)){
            valid= true;
        }
        else{
            valid=false;
        }
        return valid;
    }
}

我希望能够运行一个单独的方法来关闭它,并将ConfirmBox中的答案布尔值返回给Main方法。但是当前它返回null,因为它立即返回。

编辑:应该注意,我目前没有关闭方法。我删除了上一个,因为它引起了很多问题。

ALEJANDROMARTÍNMARQUÉS

我认为您无法做到这一点,您需要执行另一个操作,例如:

If(button.getResult==yes){"charge yesscreen")else{"charge no screen"}

差异屏幕的控制器不能与其他屏幕交互,您只能将差异屏幕从调用到控制器,但是您不能通过他传递值,请尝试执行类似的操作我以这种心态使用javafx,所有屏幕都是独立的并且您仅调用不同的屏幕。您可以执行以下操作:

  class first screen{
is a girl?
is a charizar?
is a x)
if(isgirl && is a x...){
call screen x
}else if(ismen&& is a z..){

call screen z
}


{

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

有没有一种方法可以做到这一点?

有没有更好的方法可以做到这一点(最大宽度)?

有没有其他方法可以做到这一点?

有没有一种有效的算法可以做到这一点?

有什么办法可以在PHP中更有效地做到这一点?

在公共索引上连接两个数据帧,有没有更有效的方法来做到这一点?

有没有更简单,有效或更短的方法来做到这一点?

有没有一种简单的方法可以用 lambda 来做到这一点?

PIVOT和MERGE的性能-有没有更好的方法可以做到这一点?

如果我有 N 个 TextFields 或者有什么有效的方法可以做到这一点,我是否必须制作 N TextEditingControllers?

PHPMailer无法正常工作:是否有新方法可以做到这一点?

拆分和重组数据:有没有一种有效的方法来做到这一点

有没有更短的方法来做到这一点?

有没有更好的方法来做到这一点,

有没有更好的方法,性能明智的,来做到这一点?

有没有更好的方法来做到这一点?

有没有更好的方法来做到这一点?

有没有更好的方法使用枚举来做到这一点?

我可以获得代码审查吗?如果我可以更有效地做到这一点,需要帮助

将编辑差异拼合到主记录上-我可以更简单或更有效地做到这一点吗?

有什么更好的方法可以做到这一点?摩尔斯电码程序

将.sys转换为.inf文件,是否有任何软件或方法可以做到这一点?

有没有办法在 CSS 中做到这一点?

重命名所有级别的因子变量,有没有一种tidyverse的方法来做到这一点?

有没有比使用 IIFE 更好的方法来做到这一点?

有没有办法使用更好的“就地”方法来做到这一点?

如何使用变量参数同时打印两个变量?有没有更好的方法来做到这一点?

为什么这个 numpy 构造有效,是否有更正确的方法来做到这一点

xpath小写字母-是否有xpath函数可以做到这一点?