• <legend id='v3wVY'><style id='v3wVY'><dir id='v3wVY'><q id='v3wVY'></q></dir></style></legend>

      <i id='v3wVY'><tr id='v3wVY'><dt id='v3wVY'><q id='v3wVY'><span id='v3wVY'><b id='v3wVY'><form id='v3wVY'><ins id='v3wVY'></ins><ul id='v3wVY'></ul><sub id='v3wVY'></sub></form><legend id='v3wVY'></legend><bdo id='v3wVY'><pre id='v3wVY'><center id='v3wVY'></center></pre></bdo></b><th id='v3wVY'></th></span></q></dt></tr></i><div id='v3wVY'><tfoot id='v3wVY'></tfoot><dl id='v3wVY'><fieldset id='v3wVY'></fieldset></dl></div>
        <bdo id='v3wVY'></bdo><ul id='v3wVY'></ul>

      <tfoot id='v3wVY'></tfoot>

      1. <small id='v3wVY'></small><noframes id='v3wVY'>

        Java - 全局的、可重用的加载对话框

        时间:2023-06-28
          <i id='viwyx'><tr id='viwyx'><dt id='viwyx'><q id='viwyx'><span id='viwyx'><b id='viwyx'><form id='viwyx'><ins id='viwyx'></ins><ul id='viwyx'></ul><sub id='viwyx'></sub></form><legend id='viwyx'></legend><bdo id='viwyx'><pre id='viwyx'><center id='viwyx'></center></pre></bdo></b><th id='viwyx'></th></span></q></dt></tr></i><div id='viwyx'><tfoot id='viwyx'></tfoot><dl id='viwyx'><fieldset id='viwyx'></fieldset></dl></div>

          • <tfoot id='viwyx'></tfoot>
              <tbody id='viwyx'></tbody>
            • <legend id='viwyx'><style id='viwyx'><dir id='viwyx'><q id='viwyx'></q></dir></style></legend>
                <bdo id='viwyx'></bdo><ul id='viwyx'></ul>
              • <small id='viwyx'></small><noframes id='viwyx'>

                  本文介绍了Java - 全局的、可重用的加载对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试实现一个全局加载对话框...我想调用一些静态函数来显示对话框和一些静态函数来关闭它.同时我在主线程或子线程中做一些工作......

                  I'm trying to implement a global loading dialog... I want to call some static function to show the dialog and some static function to close it. In the meanwhile I'm doing some work in the main thread or in a sub thread...

                  我尝试关注,但对话框没有更新...最后一次,在再次隐藏之前,它会更新...

                  I tried following, but the dialog is not updating... Just once at the end, before hiding it again, it updates...

                      private static Runnable getLoadingRunable()
                  {
                      if (loadingRunnable != null)
                      {
                          loadingFrame.setLocationRelativeTo(null);
                          loadingFrame.setVisible(true);  
                          return loadingRunnable;
                      }
                  
                      loadingFrame = new JFrame("Updating...");
                      final JProgressBar progressBar = new JProgressBar();
                      progressBar.setIndeterminate(true);
                      final JPanel contentPane = new JPanel();
                      contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                      contentPane.setLayout(new BorderLayout());
                      contentPane.add(new JLabel("Updating..."), BorderLayout.NORTH);
                      contentPane.add(progressBar, BorderLayout.CENTER);
                      loadingFrame.setContentPane(contentPane);
                      loadingFrame.pack();
                      loadingFrame.setLocationRelativeTo(null);
                      loadingFrame.setVisible(true);  
                  
                      loadingRunnable = new Runnable() {
                          public void run() {
                  
                              try {
                                  while (running) {
                                      Thread.sleep(100);
                                  }
                              } catch (InterruptedException e) {
                                  e.printStackTrace();
                              }
                  
                              SwingUtilities.invokeLater(new Runnable() {
                                  public void run() {
                                      loadingFrame.setVisible(false);
                                  }
                              });
                          }
                      };
                      return loadingRunnable;
                  }
                  
                  public static void showLoadingBar() {
                      System.out.println("showLoadingBar");
                      running = true;
                  
                      threadLoadingBar = new Thread(getLoadingRunable());
                      threadLoadingBar.start();
                  }
                  
                  public static void hideLoadingBar() {
                      System.out.println("hideLoadingBar");
                      running = false;
                      threadLoadingBar = null;
                  }
                  

                  推荐答案

                  如果没有动画,说明你正在事件调度线程中工作,同时显示加载帧.这个后台工作应该在另一个线程中完成.

                  If it doesn't animate, it means that you're doing work in the event dispatch thread while the loading frame is displayed. This background work should be done in another thread.

                  这是一个无效的示例:

                  public static void main(String[] args) throws Exception {
                      SwingUtilities.invokeLater(
                          new Runnable() {
                              @Override
                              public void run() {
                                  try {
                                      showLoadingBar();
                                      Thread.sleep(10000L); // doing work in the EDT. Prevents the frame from animating
                                      hideLoadingBar();
                                  }
                                  catch (InterruptedException e) {
                                  }
                              }
                          }
                      );
                  }
                  

                  这是一个工作示例:

                  public static void main(String[] args) throws Exception {
                      showLoadingBar();
                      Thread.sleep(10000L); // doing work outside of the EDT. Everything works fine
                      hideLoadingBar();
                  }
                  

                  附注:实例化、填充和使加载框架可见的代码应包装在 SwingUtilities.invokeLater() 中,因为它必须在 EDT 中运行.

                  Side note: the code which instantiates, populates and makes the loading frame visible should be wrapped into SwingUtilities.invokeLater(), because it must be run in the EDT.

                  这篇关于Java - 全局的、可重用的加载对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Lucene 搜索结果按自定义订单列表排序(每个用户唯一) 下一篇:5秒后Android关闭对话框?

                  相关文章

                1. <small id='2zrkk'></small><noframes id='2zrkk'>

                2. <legend id='2zrkk'><style id='2zrkk'><dir id='2zrkk'><q id='2zrkk'></q></dir></style></legend>

                    • <bdo id='2zrkk'></bdo><ul id='2zrkk'></ul>
                      <i id='2zrkk'><tr id='2zrkk'><dt id='2zrkk'><q id='2zrkk'><span id='2zrkk'><b id='2zrkk'><form id='2zrkk'><ins id='2zrkk'></ins><ul id='2zrkk'></ul><sub id='2zrkk'></sub></form><legend id='2zrkk'></legend><bdo id='2zrkk'><pre id='2zrkk'><center id='2zrkk'></center></pre></bdo></b><th id='2zrkk'></th></span></q></dt></tr></i><div id='2zrkk'><tfoot id='2zrkk'></tfoot><dl id='2zrkk'><fieldset id='2zrkk'></fieldset></dl></div>

                      <tfoot id='2zrkk'></tfoot>