JavaFX: Alert 弹窗

news/2024/10/3 18:56:55

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;}
}

https://dhexx.cn/news/show-591714.html

相关文章

自定义ViewGroup

a,onMeasure决定内部View的宽高&#xff0c;以及自己的宽高b,onLayout 决定子VIew的放置的位置c.onTouchEvent,动作转载于:https://www.cnblogs.com/gentspy/p/5502378.html

JavaFX:设置css

JavaFX:设置css JavaFX CSS样式参考 w3cschool JavaFX CSS CSS | JavaFX中文资料

腾讯推送 Windows 10 更新新花样 DESKTIPS.EXE

为什么80%的码农都做不了架构师&#xff1f;>>> 前段时间才刚刚报道微软取消烦人的 Windows 10 更新提示&#xff0c;现在又有了新花样&#xff01;今天早上小编打开老早的 DELL Latitude E6410 笔记本&#xff0c;启动 QQ 后发发现一个 Windows 10 更新的软件界面…

VBScript 知识体系

VBScript 知识体系 VBScript VBS是基于Visual Basic的脚本语言。VBS的全称是&#xff1a;Microsoft Visual Basic Script Edition。&#xff08;微软公司可视化BASIC脚本版&#xff09;。 其语言类似Visual Basic&#xff08;VB&#xff09; 脚本语言的特点为:简单易学 微软…

linux下使用多线程编写的聊天室

自从开始学linux网络编程后就想写个聊天室&#xff0c;一开始原本打算用多进程的方式来写&#xff0c;可是发觉进程间的通信有点麻烦&#xff0c;而且开销也大&#xff0c;后来想用多线程能不能实现呢&#xff0c;于是便去看了一下linux里线程的用法&#xff0c;实际上只需要知…

SensorService开机启动耗时探讨

SensorService开机启动耗时探讨 android11-release SensorService启动 SensorService在单独线程启动为什么SystemServer耗时 在startOtherServices中WMS需要传感器服务准备就绪 SensorDevice 连接 Hal 底层 从SensorService启动可以看到主要可能是 SensorDevice connectH…

这些HTML、CSS知识点,面试和平时开发都需要 No5-No7

系列知识点汇总 这些HTML、CSS知识点&#xff0c;面试和平时开发都需要 No1-No4&#xff08;知识点&#xff1a;HTML、CSS、盒子模型、内容布局&#xff09; 这些HTML、CSS知识点&#xff0c;面试和平时开发都需要 No5-No7&#xff08;知识点&#xff1a;文字设置、设置背景、数…

installd守护进程

installd守护进程 android11-release PackageManagerServie 服务负责应用的安装、卸载等相关工作&#xff0c;而真正干活的还是 installd。 installd 启动 Android系统启动 查看 init 进程system/core/init/init.cpp中SecondStageMain&#xff0c;最终在 LoadBootScripts 解…

Java设计模式(十一) 享元模式

2019独角兽企业重金招聘Python工程师标准>>> 原创文章&#xff0c;同步发自作者个人博客&#xff0c;http://www.jasongj.com/design_pattern/flyweight/。转载请注明出处 享元模式介绍 享元模式适用场景 面向对象技术可以很好的解决一些灵活性或可扩展性问题&#…

JavaFx:添加顶部菜单 Microsoft Ribbon For JavaFX

JavaFx:添加顶部菜单 Microsoft Ribbon For JavaFX Microsoft Ribbon For JavaFX&#xff1a;Ribbon control for Java, implemented using JavaFX, based on the Microsoft Ribbon. Github FXRibbon FXRibbon-master 导入运行 运行ChangeAccentColorSample&#xff1a;更多内…