[英]JavaFX 8 How to set program icon to alert?
How can I set program icon to alert without using alert.initOwner()
?如何在不使用
alert.initOwner()
情况下将程序图标设置为警报? Why without initOwner
?为什么没有
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.这是因为在初始化整个窗口之前必须显示一些警报,所以没有可以放入
initOwner
函数的场景。
You can steal the DialogPane from an Alert instance, and add it to a regular Stage.您可以从 Alert 实例中窃取DialogPane ,并将其添加到常规 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:
一个Node一次只能是一个Scene的root,所以需要先替换Alert的Scene的root:
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);
}
}
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");
((Stage)alert.getDialogPane().getScene().getWindow()).getIcons().add(new image("GenericApp.png"));
alert.showAndWait();
}
}
This is how it is done:这是如何完成的:
// Get the Stage.
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
// Add a custom icon.
stage.getIcons().stage.getIcons().add(new Image("images/logo_full3.png"));
There might be problem with the image reference above.上面的图片参考可能有问题。 But you can try to configure as long as it works.
但是你可以尝试配置,只要它有效。 This is how I do (I use maven).
这就是我的方式(我使用 maven)。 Yours might be different if you are not using maven.
如果您不使用 maven,您的情况可能会有所不同。
Full tutorial here: Alert javafx tutorial完整教程在这里:警报 javafx 教程
The correct implementation is following comments above:正确的实现如下上面的评论:
// Get the Stage.
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
// Add a custom icon.
stage.getIcons().add(new Image(getClass().getResourceAsStream("images/logo_full3.png")));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.