如何在不使用alert.initOwner()的情况下将程序图标设置为警报?为什么没有initOwner?这是因为在初始化整个窗口之前必须显示一些警告,所以没有场景可以放到initOwner函数中。
发布于 2017-04-20 13:55:53
您可以从警报实例中窃取DialogPane,并将其添加到常规阶段。节点一次只能是一个场景的根,因此您需要首先替换警报场景的根:
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);
}
}发布于 2017-07-08 03:39:46
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();
}
}发布于 2017-07-28 02:47:13
这是如何做到的:
// 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"));上面的图像引用可能有问题。但只要它正常工作,就可以尝试配置。我就是这样做的(我使用maven)。如果您不使用maven,您的可能会有所不同。
这里的完整教程:提醒javafx教程
https://stackoverflow.com/questions/43505213
复制相似问题