输出在进度监视器弹出之前发生

时间:2015-05-11 05:08:17

标签: java progressmonitor

我正在学习通过以下链接为我的程序构建Progress Monitor http://www.java2s.com/Tutorial/Java/0240__Swing/ProgressMonitorandSwingworker.htm

我意识到我的程序会在进度监视器弹出之前在控制台上打印1-12,如下面的屏幕截图所示。

enter image description here

一旦完成就会打印2x(1-16)。

enter image description here

如何纠正上面提到的问题?

import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ProgressMonitor;
import javax.swing.SwingWorker;

public class ProgressMonitorDemo extends JPanel implements ActionListener,
PropertyChangeListener {

    private ProgressMonitor progressMonitor;
    private JButton startButton;
    private JTextArea taskOutput;
    private Task task;

    class Task extends SwingWorker<Void, Void> {
        @Override
        public Void  doInBackground() {
            int progress = 0;
            setProgress(0);
            while (progress < 100 && !isCancelled()) 
            {   

                progress += 0;
                setProgress(Math.min(progress, 100));
                System.out.println("1 ");
                System.out.println("2 ");
                System.out.println("3 ");
                System.out.println("4 ");

                try {
                    Thread.sleep(2000);
                }catch(InterruptedException ex) {
                    ex.printStackTrace();
                }
                System.out.println("5 ");
                System.out.println("6 ");
                System.out.println("7 ");
                System.out.println("8 ");

                try {
                    Thread.sleep(2000);
                }catch(InterruptedException ex) {
                    ex.printStackTrace();
                }
                System.out.println("9 ");
                System.out.println("10 ");
                System.out.println("11 ");
                System.out.println("12 ");

                progress += 25;
                setProgress(Math.min(progress, 100));

                try {
                    Thread.sleep(2000);
                }catch(InterruptedException ex) {
                    ex.printStackTrace();
                }

                System.out.println("13 ");
                System.out.println("14 ");
                System.out.println("15 ");
                System.out.println("16 ");

                try {
                    Thread.sleep(2000);
                }catch(InterruptedException ex) {
                    ex.printStackTrace();
                }
                progress += 25;
                setProgress(Math.min(progress, 100));
            }
            return null;
        }

        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            startButton.setEnabled(true);
            progressMonitor.setProgress(0);
        }

    }

    public ProgressMonitorDemo() {
        super(new BorderLayout());

        // Create the demo's UI.
        startButton = new JButton("Start");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);

        taskOutput = new JTextArea(5, 20);
        taskOutput.setMargin(new Insets(5, 5, 5, 5));
        taskOutput.setEditable(false);

        add(startButton, BorderLayout.PAGE_START);
        add(new JScrollPane(taskOutput), BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    }


    public void actionPerformed(ActionEvent evt) {
        task = new Task();
        progressMonitor = new ProgressMonitor(ProgressMonitorDemo.this,
                "Running a Long Task", "", 0, 100);
        progressMonitor.setProgress(0);
        //progressMonitor. setMillisToPopup(0);
        task.addPropertyChangeListener(this);
        task.execute();
        startButton.setEnabled(false);
    }


    public void propertyChange(PropertyChangeEvent evt) {
        String message = " ";
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            progressMonitor.setProgress(progress);
            if(progress == 25) {
                message = String.format("1", progress);
            }
            if(progress == 50) {
                message = String.format("2", progress);
            }

            if(progress == 75) {
                message = String.format("3", progress);
            }

            if(progress == 99) {
                message = String.format("4", progress);
            }


            if(progress == 100) {
                message = String.format("5", progress);
            }

            progressMonitor.setNote(message);
            taskOutput.append(message);
            if (progressMonitor.isCanceled() || task.isDone()) {
                Toolkit.getDefaultToolkit().beep();
                if (progressMonitor.isCanceled()) {
                    task.cancel(true);
                    taskOutput.append("Task canceled.\n");
                } else {
                    taskOutput.append("Task completed.\n");
                }
                startButton.setEnabled(true);
            }
        }

    }


    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ProgressMonitorDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new ProgressMonitorDemo();
        newContentPane.setOpaque(true); 
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

它按设计工作。来自Javadoc

  

如果操作看起来需要一段时间,则会弹出一个进度对话框。