本文介绍了JavaFX 8如何设置程序图标以发出警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在不使用alert.initOwner()
的情况下将程序图标设置为警报?为什么没有initOwner
?这是因为在初始化整个窗口之前必须显示一些警报,因此我无法将任何场景放置到initOwner
函数中.
How can I set program icon to alert without using alert.initOwner()
?Why without initOwner
? It's because some alert must be shown before whole window is initialized, so there's no scene that I can put to initOwner
function.
推荐答案
您可以窃取 DialogPane ,并将其添加到常规Stage中.一个节点一次只能是一个场景的根,因此您需要先替换警报场景的根:
You can steal the DialogPane from an Alert instance, and add it to a regular Stage. A Node can only be the root of one Scene at a time, so you need to replace the root of the Alert’s Scene first:
public class AlertWithIcon
extends Application {
@Override
public void start(Stage stage) {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION,
"Are you sure you want to delete this item?",
ButtonType.YES, ButtonType.NO);
alert.setHeaderText("Delete Item");
DialogPane pane = alert.getDialogPane();
ObjectProperty<ButtonType> result = new SimpleObjectProperty<>();
for (ButtonType type : pane.getButtonTypes()) {
ButtonType resultValue = type;
((Button) pane.lookupButton(type)).setOnAction(e -> {
result.set(resultValue);
pane.getScene().getWindow().hide();
});
}
pane.getScene().setRoot(new Label());
Scene scene = new Scene(pane);
Stage dialog = new Stage();
dialog.setScene(scene);
dialog.setTitle("Delete Item");
dialog.getIcons().add(new Image("GenericApp.png"));
result.set(null);
dialog.showAndWait();
System.out.println("Result is " + result);
}
}
这篇关于JavaFX 8如何设置程序图标以发出警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!