JavaFX:窗体显示状态,模态非模态

末蓝、 2023-10-16 13:32 130阅读 0赞

程序窗体显示一般有3中模式。非模态和模态,其中模态又分为程序模态和窗体模态。

非模态可以理解为窗体之间没有任何限制,可以用鼠标、键盘等工具在窗体间切换。

程序模态是窗体打开后,该程序的所有窗体都被冻结,无法切换,只能在程序模态窗体关闭后才可切换。

窗体模态是窗体打开后,该窗体上溯的所有窗体都被冻结,无法切换,但是程序中已打开,不在该上溯线上的窗体可以和该窗体自由切换。

测试代码如下:

  1. package javafx8.ch04;
  2. import javafx.application.Application;
  3. import javafx.event.ActionEvent;
  4. import javafx.event.EventHandler;
  5. import javafx.scene.Cursor;
  6. import javafx.scene.Scene;
  7. import javafx.scene.control.Button;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.input.MouseEvent;
  10. import javafx.scene.layout.VBox;
  11. import javafx.stage.Modality;
  12. import javafx.stage.Stage;
  13. import javafx.stage.Window;
  14. import static javafx.stage.Modality.APPLICATION_MODAL;
  15. import static javafx.stage.Modality.NONE;
  16. import static javafx.stage.Modality.WINDOW_MODAL;
  17. /**
  18. * @copyright 2023-2022
  19. * @package learnjavafx8.javafx8.ch04
  20. * @file StageModalityApplication.java
  21. * @date 2023-02-02 20:59
  22. * @author qiao wei
  23. * @version 1.0
  24. * @brief 设置窗体模态,JavaFX共分3中模态。
  25. * 非模态NONE:Defines a top-level window that is not modal and does not block any other window。
  26. * 程序模态APPLICATION_MODAL:Defines a modal window that blocks events from being delivered to any
  27. * other application window。
  28. * 窗体模态WINDOW_MODAL:Defines a modal window that block events from being delivered to its entire
  29. * owner window hierarchy。
  30. * @history
  31. */
  32. public class StageModalityApplication extends Application {
  33. @Override
  34. public void start(Stage primaryStage) {
  35. // Buttons to display each kind of modal stage
  36. Button ownedNoneButton = new Button("Owned None");
  37. /**
  38. * 父控件为primaryStage,父控件与子控件stage之间为非模态,鼠标可以在父子控件间切换,当父控件关闭时,子控件
  39. * 跟随父控件一起关闭。
  40. */
  41. ownedNoneButton.addEventHandler(MouseEvent.MOUSE_CLICKED,
  42. new EventHandler<MouseEvent>() {
  43. @Override
  44. public void handle(MouseEvent event) {
  45. showDialog(primaryStage, NONE);
  46. }
  47. }
  48. );
  49. // ownedNoneButton.setOnAction(e -> showDialog(primaryStage, NONE));
  50. // ownedNoneButton.setOnAction(new EventHandler<ActionEvent>() {
  51. // @Override
  52. // public void handle(ActionEvent actionEvent) {
  53. // showDialog(primaryStage, NONE);
  54. // }
  55. // });
  56. Button nonOwnedNoneButton = new Button("Non-owned None");
  57. /**
  58. * 父控件为null, primaryStage与控件stage之间为非模态,鼠标可以在两个控件间切换,当primaryStage控件关闭
  59. * 时,stage控件不关闭。
  60. */
  61. nonOwnedNoneButton.setOnAction(e -> showDialog(null, NONE));
  62. // 与创建此窗口的上层窗口形成模态。
  63. Button ownedWinButton = new Button("Owned Window Modal");
  64. // 父控件为primaryStage的WINDOW_MODAL模式,只与父控件形成模态,与父控件的其余子控件不形成模态。
  65. ownedWinButton.setOnAction(e -> showDialog(primaryStage, WINDOW_MODAL));
  66. Button nonOwnedWinButton = new Button("Non-owned Window Modal");
  67. // 父控件为null的WINDOW_MODAL模式,因为父控件为null,相当与父控件、父控件的其余子控件不形成模态。
  68. nonOwnedWinButton.setOnAction(e -> showDialog(null, WINDOW_MODAL));
  69. // 与创建此窗口的程序的所有窗口均形成模态。
  70. Button ownedAppButton = new Button("Owned Application Modal");
  71. // Set button opacity。
  72. ownedAppButton.setOpacity(0.3);
  73. // 父控件为primaryStage的APPLICATION_MODAL模式,与Application中的的其余所有子控件均形成模态。
  74. ownedAppButton.setOnAction(e -> showDialog(primaryStage, APPLICATION_MODAL));
  75. Button nonOwnedAppButton = new Button("Non-owned Application Modal");
  76. // 父控件为null的APPLICATION_MODAL模式,与父控件、父控件的其余子控件均形成模态。
  77. nonOwnedAppButton.setOnAction((ActionEvent event)
  78. -> showDialog(null, APPLICATION_MODAL));
  79. VBox root = new VBox();
  80. root.getChildren().addAll(ownedNoneButton,
  81. nonOwnedNoneButton,
  82. ownedWinButton,
  83. nonOwnedWinButton,
  84. ownedAppButton,
  85. nonOwnedAppButton);
  86. Scene scene = new Scene(root, 300, 200);
  87. primaryStage.setScene(scene);
  88. primaryStage.setTitle("The Primary Stage");
  89. // Set full screen and unresizable。
  90. primaryStage.setFullScreen(true);
  91. // primaryStage.setResizable(false);
  92. primaryStage.show();
  93. }
  94. public static void main(String[] args) {
  95. Application.launch(StageModalityApplication.class, args);
  96. }
  97. /**
  98. * @class StageModalityApplication
  99. * @date 2023-06-21 21:01
  100. * @author qiao wei
  101. * @version 1.0
  102. * @brief 根据窗口拥有者和模式设置窗口状态。
  103. * @param owner 窗口的父控件。
  104. * @param modality 窗口模式。
  105. * @return
  106. * @throws
  107. */
  108. private void showDialog(Window owner, Modality modality) {
  109. // Create a new stage with specified owner and modality
  110. Stage stage = new Stage();
  111. // Set the stage owner and modality
  112. stage.initOwner(owner);
  113. stage.initModality(modality);
  114. Label modalityLabel = new Label(modality.toString());
  115. Button closeButton = new Button("Close");
  116. // closeButton.setOnAction(e -> stage.close());
  117. closeButton.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEvent -> stage.close());
  118. VBox root = new VBox();
  119. root.getChildren().addAll(modalityLabel, closeButton);
  120. Scene scene = new Scene(root, 200, 100);
  121. // 设置鼠标在scene的显示模式
  122. scene.setCursor(Cursor.HAND);
  123. stage.setScene(scene);
  124. stage.setTitle("A Dialog Box");
  125. stage.show();
  126. }
  127. }

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Qt

    模态是指当子窗口弹出时,焦点始终被强行集中于子窗口,只要子窗口不退出,焦点就不会被释放。非模态正好相反。 从线程角度讲,模态子窗口的线程是阻塞的,父、子窗口各有一个线程,当创

    相关 C# 详细介绍

    摘要:本文阐述了在基于.NET平台的Windows程序开发中使用模态窗体的诸多方面,部分内容延伸到一般窗体的应用。 概述   何谓模态窗体?简单的可以理解为窗体对话框,

    相关

    模态窗 开发工具与关键技术:Visual Studio 2015 – 模态窗 作者:廖亚星 撰写时间:2019年 5月 25 日 在搭建页面的时候,往往会弹