ProgressMonitor 和 ProgressMonitorInputStream都可以import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public class ProgressMonitorDemo extends JFrame {
    public final static int ONE_SECOND = 1000;    private ProgressMonitor progressMonitor;
    private Timer timer;
    private JButton startButton;
    private LongTask task;
    private JTextArea taskOutput;
    private String newline = "\n";    public ProgressMonitorDemo() {
        super("ProgressMonitorDemo");
        task = new LongTask();        //Create the demo's UI.
        startButton = new JButton("Start");
        startButton.setActionCommand("start");
        startButton.addActionListener(new ButtonListener());        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5,5,5,5));
        taskOutput.setEditable(false);        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(startButton, BorderLayout.NORTH);
        contentPane.add(new JScrollPane(taskOutput), BorderLayout.CENTER);
        contentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        setContentPane(contentPane);        //Create a timer.
        timer = new Timer(ONE_SECOND, new TimerListener());
    }    /**
     * The actionPerformed method in this class
     * is called each time the Timer "goes off".
     */
    class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            if (progressMonitor.isCanceled() || task.done()) {
                progressMonitor.close();
                task.stop();
                Toolkit.getDefaultToolkit().beep();
                timer.stop();
                if (task.done()) {
                    taskOutput.append("Task completed." + newline);
                }
                startButton.setEnabled(true);
            } else {
                progressMonitor.setNote(task.getMessage());
                progressMonitor.setProgress(task.getCurrent());
                taskOutput.append(task.getMessage() + newline);
                taskOutput.setCaretPosition(
                    taskOutput.getDocument().getLength());
            }
        }
    }    /**
     * The actionPerformed method in this class
     * is called when the user presses the start button.
     */
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            progressMonitor = new ProgressMonitor(ProgressMonitorDemo.this,
                                      "Running a Long Task",
                                      "", 0, task.getLengthOfTask());
            progressMonitor.setProgress(0);
            progressMonitor.setMillisToDecideToPopup(2 * ONE_SECOND);            startButton.setEnabled(false);
            task.go();
            timer.start();
        }
    }
    
    public static void main(String[] args) {
        JFrame frame = new ProgressMonitorDemo();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });        frame.pack();
        frame.setVisible(true);
    }
}

解决方案 »

  1.   

    /** Uses a SwingWorker to perform a time-consuming (and utterly fake) task. */public class LongTask {
        private int lengthOfTask;
        private int current = 0;
        private String statMessage;    LongTask() {
            //Compute length of task...
            //In a real program, this would figure out
            //the number of bytes to read or whatever.
            lengthOfTask = 1000;
        }    /**
         * Called from ProgressBarDemo to start the task.
         */
        void go() {
            current = 0;
            final SwingWorker worker = new SwingWorker() {
                public Object construct() {
                    return new ActualTask();
                }
            };
            worker.start();
        }    /**
         * Called from ProgressBarDemo to find out how much work needs
         * to be done.
         */
        int getLengthOfTask() {
            return lengthOfTask;
        }    /**
         * Called from ProgressBarDemo to find out how much has been done.
         */
        int getCurrent() {
            return current;
        }    void stop() {
            current = lengthOfTask;
        }    /**
         * Called from ProgressBarDemo to find out if the task has completed.
         */
        boolean done() {
            if (current >= lengthOfTask)
                return true;
            else
                return false;
        }    String getMessage() {
            return statMessage;
        }    /**
         * The actual long running task.  This runs in a SwingWorker thread.
         */
        class ActualTask {
            ActualTask () {
                //Fake a long task,
                //making a random amount of progress every second.
                while (current < lengthOfTask) {
                    try {
                        Thread.sleep(1000); //sleep for a second
                        current += Math.random() * 100; //make some progress
                        if (current > lengthOfTask) {
                            current = lengthOfTask;
                        }
                        statMessage = "Completed " + current +
                                      " out of " + lengthOfTask + ".";
                    } catch (InterruptedException e) {}
                }
            }
        }
    }
      

  2.   

    import javax.swing.SwingUtilities;/**
     * This is the 3rd version of SwingWorker (also known as
     * SwingWorker 3), an abstract class that you subclass to
     * perform GUI-related work in a dedicated thread.  For
     * instructions on using this class, see:
     * 
     * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
     *
     * Note that the API changed slightly in the 3rd version:
     * You must now invoke start() on the SwingWorker after
     * creating it.
     */
    public abstract class SwingWorker {
        private Object value;  // see getValue(), setValue()
        private Thread thread;    /** 
         * Class to maintain reference to current worker thread
         * under separate synchronization control.
         */
        private static class ThreadVar {
            private Thread thread;
            ThreadVar(Thread t) { thread = t; }
            synchronized Thread get() { return thread; }
            synchronized void clear() { thread = null; }
        }    private ThreadVar threadVar;    /** 
         * Get the value produced by the worker thread, or null if it 
         * hasn't been constructed yet.
         */
        protected synchronized Object getValue() { 
            return value; 
        }    /** 
         * Set the value produced by worker thread 
         */
        private synchronized void setValue(Object x) { 
            value = x; 
        }    /** 
         * Compute the value to be returned by the <code>get</code> method. 
         */
        public abstract Object construct();    /**
         * Called on the event dispatching thread (not on the worker thread)
         * after the <code>construct</code> method has returned.
         */
        public void finished() {
        }    /**
         * A new method that interrupts the worker thread.  Call this method
         * to force the worker to stop what it's doing.
         */
        public void interrupt() {
            Thread t = threadVar.get();
            if (t != null) {
                t.interrupt();
            }
            threadVar.clear();
        }    /**
         * Return the value created by the <code>construct</code> method.  
         * Returns null if either the constructing thread or the current
         * thread was interrupted before a value was produced.
         * 
         * @return the value created by the <code>construct</code> method
         */
        public Object get() {
            while (true) {  
                Thread t = threadVar.get();
                if (t == null) {
                    return getValue();
                }
                try {
                    t.join();
                }
                catch (InterruptedException e) {
                    Thread.currentThread().interrupt(); // propagate
                    return null;
                }
            }
        }
        /**
         * Start a thread that will call the <code>construct</code> method
         * and then exit.
         */
        public SwingWorker() {
            final Runnable doFinished = new Runnable() {
               public void run() { finished(); }
            };        Runnable doConstruct = new Runnable() { 
                public void run() {
                    try {
                        setValue(construct());
                    }
                    finally {
                        threadVar.clear();
                    }                SwingUtilities.invokeLater(doFinished);
                }
            };        Thread t = new Thread(doConstruct);
            threadVar = new ThreadVar(t);
        }    /**
         * Start the worker thread.
         */
        public void start() {
            Thread t = threadVar.get();
            if (t != null) {
                t.start();
            }
        }
    }
      

  3.   

    ProgressMonitorInputStream感觉快点
    不过是主观感觉:)