Javafx Alert 导致程序卡住

标签 java javafx error-handling alert freeze

我使用 JavaFx (.fxml) 和 Model View Controller 架构创建了一个 mp3 程序。

我想在用户单击“关于此程序”时创建一个警报,告诉他们有关该程序的一些信息。

我遇到的问题是,现在只要按下按钮,光标就会开始旋转,并且我无法再单击用户界面中的任何按钮。

我尝试了在网上找到的多个代码片段,但它们都会导致我的程序卡住。

这是我的 .fxml 文件和 Controller 中的函数。知道为什么会结冰吗?

<center>
<TitledPane fx:id="titledPane" animated="false" maxHeight="1.7976931348623157E308" text="Music">
  <BorderPane>
    <center>
      <ListView fx:id="songList" prefHeight="200.0" prefWidth="200.0" />
    </center>
    <top>
      <HBox alignment="CENTER" padding="$x1" prefHeight="-1.0" prefWidth="-1.0" spacing="15.0" BorderPane.alignment="CENTER">
        <children>
          <Label fx:id="DirLabel" text="" wrapText="true" HBox.hgrow="NEVER" />
          <Button mnemonicParsing="false" onAction="#selectDir" text="Browse..." />
          <Button onAction="#alert" text="About this Program" />
        </children>
      </HBox>
    </top>
  </BorderPane>
</TitledPane>

Controller 文件

//about the program alert
    @FXML
    private void alert(){
        JOptionPane.showMessageDialog(null, "thank you for using java");
    }

最佳答案

除非确实必要,否则不要将 Swing 组件(例如 JOptionPane)与 JavaFX 混合使用。如果确实需要,您必须从 AWT 事件调度线程管理 swing 组件,并从 FX 应用程序线程管理 JavaFX 节点,这会变得非常棘手。您的应用程序挂起的原因是您试图从 FX 应用程序线程显示一个 Swing 窗口,这(在某些平台上)会导致死锁(两个线程在继续之前相互等待)。

相反,请使用 Alert :

@FXML
private void alert() {
    new Alert(AlertType.INFORMATION, "thank you for using JavaFX").showAndWait();
}

关于Javafx Alert 导致程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231655/

相关文章:

通过 Java Web Start 部署 JavaFX - 加载进度条

java - 如何在桌面应用程序中使用 CSS 样式使 Javafx 中的文本或图像闪烁?

java - Java中通过反射调用被调用方法的返回方法

Java 同步时区列表与 moment-timezone.js

javafx - 透明图像中的javafx鼠标事件

php - PHP-过滤错误消息

mysql - 处理 Django form.save() ValueError

c# - 错误处理 : Exceptions vs Error method

java - 如何从 SBT 运行 DataNucleus Bytecode Enhancer?

java.sql.SQLException : ORA-06502: PL/SQL: numeric or value error: character to number conversio 异常