java Swing 进度条 时间进度条

项目中用到了java swing 的时间进度滚动条,所以写了一个 测试类,供大家参考,下面是 具体的实现类
package com.original.client.widget.execcenter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.miginfocom.swing.MigLayout;

/**
 *
 * @author panghui 2013-6-28
 * 消息中心倒计时进度条
 */
public class TestOExecProgressBar implements ActionListener, ChangeListener  {

    JFrame frame = null;
    JProgressBar progressbar;
    JLabel label;
    Timer timer;
    JButton b;
    JButton s;

    public TestOExecProgressBar() {
        frame = new JFrame("安装");
        frame.setBounds(100, 100, 400, 130);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPanel = frame.getContentPane();

        label = new JLabel("", JLabel.CENTER);
        progressbar = new JProgressBar();
        progressbar.setOrientation(JProgressBar.HORIZONTAL);
        progressbar.setMinimum(0);
        progressbar.setMaximum(100);
        progressbar.setValue(0);
        //progressbar.setStringPainted(true);//设置进度条上边是否显示 百分比
        progressbar.addChangeListener(this);// 实现进度条值改变的动态监听
        progressbar.setBounds(0, 20, 100, 5);
        progressbar.setBorderPainted(false);
        progressbar.setForeground(new Color(55,165,228));
        progressbar.setBackground(new Color(218,218,218));

        JPanel panel = new JPanel(new MigLayout("wrap 2","[grow,100%]","[grow,50%] [grow,50%]"));
        panel.setBackground(new Color(255,255,255));
        panel.setPreferredSize(new Dimension(400,50));
        b = new JButton("安装");
        b.setForeground(Color.blue);
        b.addActionListener(this);
        s = new JButton("停止");
        s.setForeground(Color.blue);
        s.addActionListener(this);
        panel.add(b);
        panel.add(s);
        panel.add(progressbar,"growx,h 7!,span 2");
        timer = new Timer(100, this);
        contentPanel.add(panel, BorderLayout.NORTH);
        contentPanel.add(label, BorderLayout.SOUTH);
        frame.setVisible(true);


    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            timer.start();
        }
        if (e.getSource() == s) {
            timer.stop();
        }
        if (e.getSource() == timer) {
            int value = progressbar.getValue();
            if (value < 100) {
                progressbar.setValue(++value);
            } else {
                timer.stop();
                //frame.dispose();
            }
        }

    }

    public void stateChanged(ChangeEvent e1) {
        int value = progressbar.getValue();
        if (e1.getSource() == progressbar) {
            label.setText("目前已完成进度:" + Integer.toString(value) + "%");
            label.setForeground(Color.blue);
        }
    }

    public static void main(String[] args) {
        TestOExecProgressBar app = new TestOExecProgressBar();
    }
}

java Swing 进度条 时间进度条_第1张图片

你可能感兴趣的:(java,java,swing,时间进度)