猿问

如何使我的SwingWorker示例正常工作?

我制作了自己的SwingWorker示例,以熟悉其工作原理。


我要执行的操作如下:单击按钮时,我希望进度条出现直到任务完成,我只想删除进度条并向对话框添加字符串。


单击该按钮时,进度条会出现,但永远不会消失。(千万不要在10秒后删除进度条,也不要将标签放在上方)


这是一个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;

    }



UYOU
浏览 498回答 3
3回答

翻翻过去那场雪

这是您的代码的更新版本,可以使用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 {&nbsp; public static void main(String[] args) {&nbsp; &nbsp; EventQueue.invokeLater( new Runnable() {&nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; JButton button = new JButton();&nbsp; &nbsp; &nbsp; &nbsp; button.addActionListener(new ActionListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void actionPerformed(ActionEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new GuiWorker().execute();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; button.setText("Test Me");&nbsp; &nbsp; &nbsp; &nbsp; frame.getContentPane().add(button);&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } );&nbsp; }}class GuiWorker extends SwingWorker<Integer, Integer> {&nbsp; /*&nbsp; * This should just create a frame that will hold a progress bar until the&nbsp; * work is done. Once done, it should remove the progress bar from the dialog&nbsp; * and add a label saying the task complete.&nbsp; */&nbsp; private JFrame frame = new JFrame();&nbsp; private JDialog dialog = new JDialog(frame, "Swingworker test", true);&nbsp; private JProgressBar progressBar = new JProgressBar();&nbsp; public GuiWorker() {&nbsp; &nbsp; progressBar.setString("Waiting on time");&nbsp; &nbsp; progressBar.setStringPainted(true);&nbsp; &nbsp; progressBar.setIndeterminate(true);&nbsp; &nbsp; dialog.getContentPane().add(progressBar);&nbsp; &nbsp; dialog.pack();&nbsp; &nbsp; dialog.setModal( false );&nbsp; &nbsp; dialog.setVisible(true);&nbsp; }&nbsp; @Override&nbsp; protected Integer doInBackground() throws Exception {&nbsp; &nbsp; System.out.println( "GuiWorker.doInBackground" );&nbsp; &nbsp; Thread.sleep(1000);&nbsp; &nbsp; return 0;&nbsp; }&nbsp; @Override&nbsp; protected void done() {&nbsp; &nbsp; System.out.println("done");&nbsp; &nbsp; JLabel label = new JLabel("Task Complete");&nbsp; &nbsp; dialog.getContentPane().remove(progressBar);&nbsp; &nbsp; dialog.getContentPane().add(label);&nbsp; &nbsp; dialog.getContentPane().validate();&nbsp; }}关键是设置模型对话框可见块,直到对话框被处理为止。因此,validate在切换组件时,使其成为非模式固定+ 内容窗格上的调用。我还调整了您的主要方法以在EDT上运行,并添加了一些System.out调用。如果删除setModal( false )呼叫,您将看到在关闭对话框之前不会打印这些语句
随时随地看视频慕课网APP

相关分类

Java
我要回答