Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaFX中显示来自主类的阶段_Java_Javafx_Javafx 8 - Fatal编程技术网

在JavaFX中显示来自主类的阶段

在JavaFX中显示来自主类的阶段,java,javafx,javafx-8,Java,Javafx,Javafx 8,我是javafx新手,需要你的帮助。我在网上搜索,但没有成功。。。所以,当我第一次使用jar时,我试图使一个对话框出现。为此,我尝试测试文件中是否存在某个属性,如果存在,则启动“首次对话框”。单击此对话框上的“确定”后,我会将属性保存到文件中。唯一的问题是,初级阶段是“主窗口”,而不是第一次。这个对话框有一个控制器,所以当我在按钮的“setOnAction方法”中时,我不能启动primaryStage。我不能使用“node.getWindow.getScene”(或者类似的)方法,因为它还没有打

我是javafx新手,需要你的帮助。我在网上搜索,但没有成功。。。所以,当我第一次使用jar时,我试图使一个对话框出现。为此,我尝试测试文件中是否存在某个属性,如果存在,则启动“首次对话框”。单击此对话框上的“确定”后,我会将属性保存到文件中。唯一的问题是,初级阶段是“主窗口”,而不是第一次。这个对话框有一个控制器,所以当我在按钮的“setOnAction方法”中时,我不能启动primaryStage。我不能使用“node.getWindow.getScene”(或者类似的)方法,因为它还没有打开,用户仍然在“第一时间窗口”中。这是我的密码

public void start(Stage primaryStage) throws Exception{

    Parent root = FXMLLoader.load(getClass().getResource("real.fxml"));
    primaryStage.setTitle("LoL Ready To Win");
    primaryStage.setResizable(false);
    primaryStage.setScene(new Scene(root));

    try {

        input = new FileInputStream("config.properties");
        prop.load(input);

        if(prop.getProperty("name") != null){
            primaryStage.show();
        }else if(prop.getProperty("name") == null){

            try {
                Stage stage = new Stage();
                Parent root1 = FXMLLoader.load(getClass().getResource("summ.fxml"));
                stage.setTitle("Insert Title Here");
                stage.setResizable(false);
                stage.setScene(new Scene(root1));
                stage.show();
            }catch(IOException e){
                e.printStackTrace();
            }
        }


    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
这是控制器

submit.setOnAction(e->{
    if (summfield.getText() == null || summfield.getText().trim().isEmpty()) {
        Alert alert = new Alert(Alert.AlertType.INFORMATION);
        alert.setTitle("Temp title");
        alert.setHeaderText(null);
        alert.setContentText("Please enter a valid name!");
        alert.showAndWait();
    }else{
        try {

            output = new FileOutputStream("config.properties");

            prop.setProperty("name", summfield.getText());

            prop.store(output, null);

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException a) {
                    a.printStackTrace();
                }
            }

        }

        // And this is where I wanna launch the primaryStage
    }

});

如果你有任何建议,即使它不是我问题的答案,我也很感激!谢谢大家。

您不能更改发布顺序吗

public void start(Stage primaryStage) {
    Parent root = FXMLLoader.load(getClass().getResource("real.fxml"));
    primaryStage.setTitle("LoL Ready To Win");
    primaryStage.setResizable(false);
    primaryStage.setScene(new Scene(root));

    try {

        input = new FileInputStream("config.properties");
        prop.load(input);

        if(prop.getProperty("name") == null){               
            try {
                Stage stage = new Stage();
                Parent root1 = FXMLLoader.load(getClass().getResource("summ.fxml"));
                stage.setTitle("Insert Title Here");
                stage.setResizable(false);
                stage.setScene(new Scene(root1));
                stage.showAndWait();

                input = new FileInputStream("config.properties");
                prop.load(input);
            }catch(IOException e){
                e.printStackTrace();
            }
        }

        primaryStage.show();

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

然后在show once窗口中写出您的属性?

在控制器for summ.fxml中,执行以下操作:

public class SummController {

    private String name ;

    public String getName() {
        return name ;
    }

    public void initialize() {

        submit.setOnAction(e->{
            if (summfield.getText() == null || summfield.getText().trim().isEmpty()) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("Temp title");
                alert.setHeaderText(null);
                alert.setContentText("Please enter a valid name!");
                alert.showAndWait();
            }else{
                try {

                    name = summfield.getText();

                    output = new FileOutputStream("config.properties");

                    prop.setProperty("name", name);

                    prop.store(output, null);

                    submit.getScene().getWindow().hide();

                } catch (IOException io) {
                    io.printStackTrace();
                } finally {
                    if (output != null) {
                        try {
                            output.close();
                        } catch (IOException a) {
                            a.printStackTrace();
                        }
                    }

                }

            }

        });
    }

}
现在将start方法修改为:

public void start(Stage primaryStage) throws Exception{

    Parent root = FXMLLoader.load(getClass().getResource("real.fxml"));
    primaryStage.setTitle("LoL Ready To Win");
    primaryStage.setResizable(false);
    primaryStage.setScene(new Scene(root));

    try {

        input = new FileInputStream("config.properties");
        prop.load(input);

        if(prop.getProperty("name") != null){
            primaryStage.show();
        }else if(prop.getProperty("name") == null){

            try {
                Stage stage = new Stage();

                FXMLLoader loader = new FXMLLoader(getClass().getResource("summ.fxml"));
                Parent root1 = loader.load();

                stage.setTitle("Insert Title Here");
                stage.setResizable(false);
                stage.setScene(new Scene(root1));

                stage.showAndWait();

                // if you need the input:
                SummController summController = loader.getController();
                String name = summController.getName();
                // do something with name...

                primaryStage.show();

            }catch(IOException e){
                e.printStackTrace();
            }
        }


    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // ...
}

它似乎工作正常,但唯一的问题是,如果输入不存在,它会给出一个错误。我将尝试检查它是否也存在。。。我会让你知道它是否有效。非常感谢!这很有效。我在输入声明之前添加了一个“if(file.exists()”。就像这样,它很完美。谢谢!