【发布时间】:2021-03-12 18:45:15
【问题描述】:
所以我有一个基于 Hydra 的程序。弹出一个窗口,当试图关闭它时,它会关闭,但会在其位置弹出两个窗口。有两种方法可以关闭窗口,按关闭按钮或红叉。
我的问题是程序的行为非常不可预测,有时它会关闭它并打开 2 个新的,有时它不会关闭并且不会打开一个新的。
警告!!!!如果执行此代码,则必须通过任务管理器或 IDE 终止程序。
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
this.stage.setTitle("Hail Hydra");
placeNewHeadRandomly();
this.stage.setScene(growHead());
this.stage.setOnCloseRequest(e -> {
cutOffHead();
});
this.stage.show();
}
private void placeNewHeadRandomly() {
// not important for this question but randomly changes windows X and Y.
}
private Scene growHead() {
// not important for this question creates a window with a button that calls cutOffHead();
VBox vbox = new VBox();
vbox.setPrefWidth(WINDOW_WIDTH);
vbox.setPrefHeight(WINDOW_HEIGHT);
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
Label warning = new Label("Cut off one hydra head, two more will grow back in its place.");
Button sword = new Button("close");
sword.setOnAction(e -> cutOffHead());
vbox.getChildren().addAll(warning, sword);
return new Scene(vbox);
}
private void cutOffHead() {
this.stage.close();
try {
start(new Stage());
start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】: