线程“ JavaFX Application Thread”中的异常java.lang.IllegalStateException:一旦设置了可见的阶段,就无法设置样式

时间:2019-05-30 07:51:29

标签: java javafx

我对JavaFX有一个小问题。我希望这些场景没有装饰,但我收到这样的消息。我知道发生了什么,但我不知道如何解决。 您有解决此问题的想法吗?

谢谢

错误

Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: Cannot set style once stage has been set visible
    at javafx.stage.Stage.initStyle(Stage.java:493)
    at DialogsBoxes.exitConfirmation(DialogsBoxes.java:43)
    at LaunchApplication.lambda$start$0(LaunchApplication.java:38)

DialogBoxes.java

当我注释掉这段代码时,exitStage.initStyle(StageStyle.UNDECORATED);一切都很好,但是我希望关闭的窗口没有装饰。

    @FXML
    private AnchorPane exitAnchorPane;

    @FXML
    private Button confirmExit;

    @FXML
    private Button confirmReturn;

    @FXML
    void exitConfirmation(MouseEvent event) {
        exitApplication();
    }

    @FXML
    void returnConfirmation(MouseEvent event) {
        returnToApplication();
    }

        public static Stage exitStage = new Stage();


        @FXML
        public static void exitConfirmation() throws IOException{
            Parent exitRoot = new FXMLLoader().load(DialogsBoxes.class.getResource("ExitBox.fxml"));
            Scene exitScene = new Scene(exitRoot);

            exitStage.initStyle(StageStyle.UNDECORATED);
            //exitStage.initModality(Modality.WINDOW_MODAL);
            //exitStage.initOwner(stage);
            exitStage.setScene(exitScene);

            exitStage.show();
        }

        public void exitApplication(){
            Platform.exit();
            //System.exit(0);
        }

        public void returnToApplication(){
            exitStage.close();
        }

}

LaunchApplication.java(主控制器)

public static Stage stage = null;

@Override
public void start(Stage stage) throws IOException {

    Parent root = FXMLLoader.load(getClass().getResource("LoginUI.fxml"));

    Scene scene = new Scene(root);

    stage.initStyle(StageStyle.UNDECORATED);

    stage.setScene(scene);

    this.stage = stage;

    stage.show();

    KeyCombination combination = new KeyCodeCombination(KeyCode.ESCAPE, KeyCodeCombination.SHIFT_ANY);
    scene.setOnKeyPressed(event -> {
        if(combination.match(event)){
            try {
                DialogsBoxes.exitConfirmation();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

}


public static void main(String[] args){

    launch(args);
}

1 个答案:

答案 0 :(得分:0)

initWindow() 必须在显示舞台之前调用。

例如

parentAccessAuthenticatorStage.initOwner(Main.scene.getWindow()); parentAccessAuthenticatorStage.show();