如何使我的 SwingWorker 示例正常工作? [英] How do I make my SwingWorker example work properly?

查看:24
本文介绍了如何使我的 SwingWorker 示例正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了自己的 SwingWorker 示例以熟悉它的工作原理.

I've made my own SwingWorker example to get familiar with how it works.

我想做的是以下内容:单击按钮时,我希望在任务完成之前显示进度条,我只想删除进度条并在对话框中添加一个字符串.

What I'm wanting to do is the following: When the button is clicked I want a progress bar appear until the task is done I want to simply remove the progress bar and add a string to the dialog.

点击按钮时,进度条会出现但不会消失.(永远不会在 10 秒后移除进度条,永远不会将标签放上去)

When the button is clicked, the progress bar comes up but never goes away. (never removes the progress bar after 10 seconds and never places the label up)

这是一个 SSCCE:

Here is an SSCCE:

package swingtesting;

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class SwingTesting {

    /**
     * Creates a frame that will hold a simple button to make use of SwingWorker
     */
     public static void main(String[] args) {
         // TODO code application logic here
         JFrame frame = new JFrame();
         JButton button = new JButton();

         button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new GuiWorker().execute();
            }
         });
         button.setText("Test Me");
         frame.getContentPane().add(button);
         frame.pack();
         frame.setVisible(true);
    }
}

class GuiWorker extends SwingWorker<Integer, Integer> {

    /*
     * This should just create a frame that will hold a progress bar until the
     * work is done. Once done, it should remove the progress bar from the dialog
     * and add a label saying the task complete.
     */

    private JFrame frame = new JFrame();
    private JDialog dialog = new JDialog(frame, "Swingworker test", true);
    private JProgressBar progressBar = new JProgressBar();


    public GuiWorker() {
        progressBar.setString("Waiting on time");
        progressBar.setStringPainted(true);
        progressBar.setIndeterminate(true);
        dialog.getContentPane().add(progressBar);
        dialog.pack();
        dialog.setVisible(true);
    }

    @Override
    protected Integer doInBackground() throws Exception {
        Thread.sleep(10000);
        return 0;
    }

    @Override
    protected void done() {
        JLabel label = new JLabel("Task Complete");
        dialog.getContentPane().remove(progressBar);
        dialog.getContentPane().add(label);
    }

}

推荐答案

这里是您的代码的更新版本

Here an updated version of your code which works

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class SwingTesting {

  public static void main(String[] args) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame();
        JButton button = new JButton();
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            new GuiWorker().execute();
          }
        });
        button.setText("Test Me");
        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
      }
    } );

  }
}

class GuiWorker extends SwingWorker<Integer, Integer> {

  /*
  * This should just create a frame that will hold a progress bar until the
  * work is done. Once done, it should remove the progress bar from the dialog
  * and add a label saying the task complete.
  */

  private JFrame frame = new JFrame();
  private JDialog dialog = new JDialog(frame, "Swingworker test", true);
  private JProgressBar progressBar = new JProgressBar();


  public GuiWorker() {
    progressBar.setString("Waiting on time");
    progressBar.setStringPainted(true);
    progressBar.setIndeterminate(true);
    dialog.getContentPane().add(progressBar);
    dialog.pack();
    dialog.setModal( false );
    dialog.setVisible(true);
  }

  @Override
  protected Integer doInBackground() throws Exception {
    System.out.println( "GuiWorker.doInBackground" );
    Thread.sleep(1000);
    return 0;
  }

  @Override
  protected void done() {
    System.out.println("done");
    JLabel label = new JLabel("Task Complete");
    dialog.getContentPane().remove(progressBar);
    dialog.getContentPane().add(label);
    dialog.getContentPane().validate();
  }

}

关键点是设置模型对话框可见阻塞,直到对话框被处理.因此,当您切换组件时,使其非模态固定 + 内容窗格上的 validate 调用.我还调整了您的主要方法以在 EDT 上运行,并添加了一些 System.out 调用.如果删除 setModal( false ) 调用,您将看到在关闭对话框之前不会打印这些语句

Key point is that setting a model dialog visible blocks until the dialog is disposed. So making it non-modal fixed it + the validate call on the content pane when you switch components. I also adjusted your main method to run on the EDT, and added some System.out calls. If you remove the setModal( false ) call you will see those statements are not printed until you close the dialog

这篇关于如何使我的 SwingWorker 示例正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆