我意识到当舞台进入隐藏模式并且通知需要在舞台中显示时通知组件找不到舞台时会发生此异常。在互联网上搜索后,我找到了解决这个问题的两种方法。
解决方案一:
打开舞台并显示通知。
这样我们应该检查舞台是否被隐藏,打开它并显示通知。
为此,我们必须在 CreateNotification 方法中添加此条件:
Stage stage = (Stage) button.getScene().getWindow();
if (!stage.isShowing()){
stage.show();
}
解决方案2:
在此解决方案中,我们创建一个虚拟舞台并将其不透明度设置为零,然后隐藏主舞台。我在此链接中找到此解决方案并将代码放入here:
public void createDummyStage() {
Stage dummyPopup = new Stage();
dummyPopup.initModality(Modality.NONE);
// set as utility so no iconification occurs
dummyPopup.initStyle(StageStyle.UTILITY);
// set opacity so the window cannot be seen
dummyPopup.setOpacity(0d);
// not necessary, but this will move the dummy stage off the screen
final Screen screen = Screen.getPrimary();
final Rectangle2D bounds = screen.getVisualBounds();
dummyPopup.setX(bounds.getMaxX());
dummyPopup.setY(bounds.getMaxY());
// create/add a transparent scene
final Group root = new Group();
dummyPopup.setScene(new Scene(root, 1d, 1d, Color.TRANSPARENT));
// show the dummy stage
dummyPopup.show();
}
正如我下面提到的,我们应该在隐藏主舞台之前调用此方法:
@FXML
public void handleSysTryAction(ActionEvent event) {
Stage stage = (Stage) button.getScene().getWindow();
createDummyStage();
stage.hide();
}
我实现了这两个解决方案,一切都运行良好。如果您对这个问题有更好的解决方案请放在这里
您可以从我的网站下载完整的 Netbeans 项目Dropbox