【问题标题】:Show JDialog ("Please Wait") on JFrame在 JFrame 上显示 JDialog(“请稍候”)
【发布时间】:2012-02-05 06:15:38
【问题描述】:

我有一个JFrame,而这个JFrame 有一个JButton

我想在JButton中首先显示JDialog(显示"Please wait")并执行其他代码然后关闭JDialog

但是当显示JDialog 时停止在JButton 上执行其他代码。

【问题讨论】:

    标签: java swing jframe jdialog


    【解决方案1】:

    Thread 上开始其他处理(例如在SwingWorker 中),并在开始时调用modalDialog.setVisible(true)。任务结束时调用setVisible(false)

    【讨论】:

      【解决方案2】:

      我建议创建一个简单的 JDialog,然后在您的代码运行后处理它。您可以使用以下代码创建您的 JDialog:

      JDialog dialog = new JDialog();
      JLabel label = new JLabel("Please wait...");
      dialog.setLocationRelativeTo(null);
      dialog.setTitle("Please Wait...");
      dialog.add(label);
      dialog.pack();
      

      并像这样实现它:

      dialog.setVisible(true); // show the dialog on the screen
      
              // Do something here
      
      dialog.setVisible(false); // set visibility to false when the code has run
      

      【讨论】:

      • 恕我直言,这是最好的答案。如果不需要线程,为什么还要添加线程的额外复杂性。
      • 这很简单,但在某些情况下可能不起作用...javax.swing 需要花费大量时间来创建或绘制框架。因此,您的消息可能不可见,因为 JVM 不会等到 swing 完成其工作。该线程可能会转到//Do something here
      【解决方案3】:

      可能 jdialog 处于模态模式,尝试更改 jdialog 的 modal 属性:yordialog.setModal(false)

      【讨论】:

        【解决方案4】:

        它停止是因为您正在使用“MAIN-Thread”来执行代码并显示 JDialog。

        要解决这个问题,您应该查看类似SwingWorker

        【讨论】:

          猜你喜欢
          • 2013-12-14
          • 2012-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-12
          相关资源
          最近更新 更多