码迷,mamicode.com
首页 > 其他好文 > 详细

JProgressBar

时间:2015-07-07 00:43:30      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

 技术分享

package swing.progress;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

/*2015-7-6*/
public class ProgressBarTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frame = new ProgressBarFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                frame.setLocationRelativeTo(null);

            }
        });
    }
}

class ProgressBarFrame extends JFrame {
    private static final long serialVersionUID = 1L;

    public static final int DEFAULT_WIDTH = 400;
    public static final int DEFAULT_HEIGHT = 200;

    private JButton startButton;
    private JProgressBar progressBar;
    private JCheckBox checkBox;
    private JTextArea textArea;
    private SimulatedActivity activity;

    public ProgressBarFrame() {
        setTitle("ProgressBarTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        textArea = new JTextArea();

        final int MAX = 1000;
        JPanel panel = new JPanel();
        startButton = new JButton("Start");
        progressBar = new JProgressBar(0, MAX);
        progressBar.setStringPainted(true);
        panel.add(startButton);
        panel.add(progressBar);

        checkBox = new JCheckBox("indeterminate");
        checkBox.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                progressBar.setIndeterminate(checkBox.isSelected());
                progressBar.setStringPainted(!progressBar.isIndeterminate());
            }
        });

        panel.add(checkBox);
        add(new JScrollPane(textArea), BorderLayout.CENTER);
        add(panel, BorderLayout.SOUTH);

        startButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                startButton.setEnabled(false);
                activity = new SimulatedActivity(MAX);
                activity.execute();
            }
        });

    }

    class SimulatedActivity extends SwingWorker<Void, Integer> {

        private int current;
        private int target;

        public SimulatedActivity(int target) {
            super();
            current = 0;
            this.target = target;
        }

        @Override
        protected Void doInBackground() throws Exception {
            while (current < target) {
                TimeUnit.SECONDS.sleep(1);
                current++;
                publish(current);
                System.out.println("publish:" + current);
            }
            return null;
        }

        // 异步地从 publish 方法接收数据块
        @Override
        protected void process(List<Integer> chunks) {
            System.out.println("process.Size:" + chunks.size());
            for (Integer chunk : chunks) {
                textArea.append(chunk + "\n");
                System.out.println("process:" + chunk);
                progressBar.setValue(chunk);
            }
        }

        @Override
        protected void done() {
            startButton.setEnabled(true);
        }

    }

}

print:

publish:1
process.Size:1
process:1
publish:2
process.Size:1
process:2
...

 

JProgressBar

标签:

原文地址:http://www.cnblogs.com/softidea/p/4625691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!