java进度条时间显示怎么实现?

TheDisguiser 2020-09-15 14:55:49 java常见问答 9599

我们在一些网站中经常能够看到进度条,这是提示我们当前加载到什么时候了,本篇文章我们就来看看用java如何写出这么一个时间进度条。

示例:

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常见问题及解决方法的小伙伴就请关注我们网站吧。

推荐阅读:

java进度条功能的实现原理是什么?实例展示

java进度条重绘要怎么编写?

java进度条窗口具体如何制作?