JavaFX: Alert 弹窗
JavaFX视频教程第22课,DialogPane类和ScheduledService多任务的简单使用
JavaFX视频教程第91课,Alert 弹窗
code.makery:JavaFX Dialogs (official)
DialogPane 类
https://openjfx.cn/javadoc/16/javafx.controls/javafx/scene/control/DialogPane.html
- 标题 Stage 中设置,其他属性设置如top显示、
- DialogPane 继承Pane,需要添加到Scene、Stage中
- DialogPane 头部文字HeaderText、内容ContentText
- DialogPane 添加 button types 按钮和监听
public class DialogController {@FXMLprotected void onHelloButtonClick() {DialogPane dialog = new DialogPane();dialog.setHeaderText(" xhbruce ");dialog.setContentText(" Dialog Sampler ");dialog.getButtonTypes().add(ButtonType.APPLY);dialog.getButtonTypes().add(ButtonType.CANCEL);dialog.getButtonTypes().add(ButtonType.CLOSE);dialog.getButtonTypes().add(ButtonType.FINISH);dialog.getButtonTypes().add(ButtonType.NEXT);dialog.getButtonTypes().add(ButtonType.NO);dialog.getButtonTypes().add(ButtonType.OK);dialog.getButtonTypes().add(ButtonType.PREVIOUS);dialog.getButtonTypes().add(ButtonType.YES);Button close = (Button)dialog.lookupButton(ButtonType.CLOSE);close.setOnAction(event -> {System.out.println(" xhbruce 关闭 ");dialog.setHeaderText(" xhbruce 关闭 ");});Button apply = (Button)dialog.lookupButton(ButtonType.APPLY);apply.setOnAction(event -> {System.out.println(" xhbruce 应用 ");dialog.setHeaderText(" xhbruce 应用 ");});Stage dialogStage = new Stage();Scene dialogScene = new Scene(dialog);dialogStage.setScene(dialogScene);dialogStage.setTitle("退出!");dialogStage.initOwner(DialogApplication.PRIMARY_STAGE);dialogStage.initStyle(StageStyle.UTILITY);dialogStage.initModality(Modality.WINDOW_MODAL);//dialogStage.setAlwaysOnTop(true);dialogStage.setResizable(false);dialogStage.show();}
}
DialogPane 自定义布局
- 图片添加setGraphic
- 扩展内容setExpandableContent
ImageView imageView = new ImageView(PathInfo.getImage("kotlin-Android.PNG"));dialog.setGraphic(imageView);dialog.setExpandableContent(new Text(" 扩展内容 "));
- 添加自定义fxml布局
(界面闪烁)
Node content = PathInfo.loadfxml("expandable.fxml");dialog.setContent(content);
Alert 类
https://openjfx.cn/javadoc/16/javafx.controls/javafx/scene/control/Alert.html
AlertType 类型
public static enum AlertType {/*** The NONE alert type has the effect of not setting any default properties* in the Alert.*/NONE,/*** The INFORMATION alert type configures the Alert dialog to appear in a* way that suggests the content of the dialog is informing the user of* a piece of information. This includes an 'information' image, an* appropriate title and header, and just an OK button for the user to* click on to dismiss the dialog.*/INFORMATION,/*** The WARNING alert type configures the Alert dialog to appear in a* way that suggests the content of the dialog is warning the user about* some fact or action. This includes a 'warning' image, an* appropriate title and header, and just an OK button for the user to* click on to dismiss the dialog.*/WARNING,/*** The CONFIRMATION alert type configures the Alert dialog to appear in a* way that suggests the content of the dialog is seeking confirmation from* the user. This includes a 'confirmation' image, an* appropriate title and header, and both OK and Cancel buttons for the* user to click on to dismiss the dialog.*/CONFIRMATION,/*** The ERROR alert type configures the Alert dialog to appear in a* way that suggests that something has gone wrong. This includes an* 'error' image, an appropriate title and header, and just an OK button* for the user to click on to dismiss the dialog.*/ERROR}
- NONE
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "Are you sure you want to format your system?");alert.showAndWait();
- INFORMATION
- WARNING
- CONFIRMATION
- ERROR
Alert 相关属性
- 基础标题、头部文字HeaderText、内容ContentText、一个图标
- Button 添加并监听反馈按钮操作
- 位于stage的top,Alert显示后底部不可操作
public class DialogController {@FXMLprotected void onHelloButtonClick() {Alert alert = createAlert(AlertType.NONE," Xhbruce Alert "," Header Text "," Content Text ",null,DialogApplication.PRIMARY_STAGE,new ImageView(PathInfo.getImage("kotlin-Android.PNG")));alert.getButtonTypes().add(ButtonType.CANCEL);alert.getButtonTypes().add(ButtonType.CLOSE);ButtonType buttonTypeOne = new ButtonType("One");ButtonType buttonTypeCancel = new ButtonType("XhBruce 应用", ButtonBar.ButtonData.APPLY);alert.getButtonTypes().addAll(buttonTypeOne, buttonTypeCancel);//alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);Optional<ButtonType> result = alert.showAndWait();ButtonType buttonType = result.get();if (buttonType == ButtonType.CANCEL){System.out.println(" ButtonType.CANCEL ");} else if (buttonType == ButtonType.CLOSE) {System.out.println(" ButtonType.CLOSE ");} else if (buttonType == buttonTypeOne) {System.out.println(" ButtonTypeOne ");} else {System.out.println(" ButtonType ");}}// Alert 弹框private Alert createAlert(AlertType alertType, String title, String headerText, String contentText,StageStyle stageStyle, Stage owner, Node graphic) {Alert alert = new Alert(alertType);if (title != null) {alert.setTitle(title);}if (headerText != null) {alert.setHeaderText(headerText);}if (contentText != null) {alert.setContentText(contentText);}if (stageStyle != null) {alert.initStyle(stageStyle);}if (owner != null) {alert.initOwner(owner);alert.heightProperty().addListener(l -> {double centerX = owner.getX() + owner.getWidth() / 2;double centerY = owner.getY() + owner.getHeight() / 2;double alertWidth = alert.getWidth();double alertHeight = alert.getHeight();double alertScreenX = centerX - alert.getWidth() / 2;double alertScreenY = centerY - alert.getHeight() / 2;//System.out.println("alert : alertScreenX=" + alertScreenX + ",alertScreenY=" + alertScreenY);//System.out.println("alert : alertWidth=" + alertWidth + ",alertHeight=" + alertHeight);if (alertScreenX + alertWidth > JavaFXTool.getVisualScreenWidth())alertScreenX = (int) (JavaFXTool.getVisualScreenWidth() - alertWidth);else if (alertScreenX < 0) {alertScreenX = 0;}if (alertScreenY + alertHeight > JavaFXTool.getVisualScreenHeight()) {alertScreenY = (int) (JavaFXTool.getVisualScreenHeight() - alertHeight);} else if (alertScreenY < 0) {alertScreenY = 0;}// Set the X and Y of the Alertalert.setX(alertScreenX);alert.setY(alertScreenY);});}if (graphic != null) {alert.setGraphic(graphic);}//alert.showAndWait();return alert;}
}