回 帖 发 新 帖 刷新版面

主题:[原创]进度条制作,让大家一起分享!

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;

public class ProgressBarDemo implements ActionListener,ChangeListener
{
    JFrame f = null;
    JProgressBar progressbar;
    JLabel label;
    Timer timer;
    JButton b;

    public ProgressBarDemo()
    {
        f = new JFrame("progressbar Example");
        Container contentPane = f.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.setPreferredSize(new Dimension(200,20));
        progressbar.setBorderPainted(true);
        progressbar.setBackground(Color.pink);

        JPanel panel = new JPanel();
        b = new JButton("Start");
        b.setForeground(Color.orange);
        b.addActionListener(this);
        panel.add(b);

        timer = new Timer(50,this);

        contentPane.add(panel,BorderLayout.NORTH);
        contentPane.add(progressbar,BorderLayout.CENTER);
        contentPane.add(label,BorderLayout.SOUTH);

        f.pack();
        f.setVisible(true);

        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args)
    {
        new ProgressBarDemo();
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == b)
        {
            timer.start();
        }

        if(e.getSource() == timer)
        {
            int value = progressbar.getValue();

            if( value < 100)
            {
                value++;
                progressbar.setValue(value);
            }
            else
            {
              timer.stop();
//            new bAndW.BAndW();
//            Report.ReportComplier@1bf6770
//              new Report.ReportComplier();
              //System.out.println(new Report.ReportComplier());
              Double dValue=new Double(1233.213f);
              System.out.println(dValue);
              f.dispose();
            }
        }
    }

    public void stateChanged(ChangeEvent e1)
    {
        int value = progressbar.getValue();

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

回复列表 (共26个回复)

21 楼

支持,收藏

但有2个问题
1.这是哪年的贴子啊
2.屏保的程序好象没有正常的退出接口

22 楼

一、要加进度的程序

new GuageGUI("数据检索中,请稍候......") {
            public void doWork() {
               //执行操作是什么
            }

            public void doFinished() {
                
            }
        }.start();


比如要用一个按钮去刷新JTABEL 里的数据:


        /**
         * 刷新
         */
        ActionFactory.registerActionListener(刷新按钮, new ActionProxy() {
            public void doAction() {
                new GuageGUI("数据检索中,请稍候......") {
                    public void doWork() {
                        try {
                            //根据条件查找数据,可以是用JTABEL显示数据等。
                        } catch (Exception ex) {
                            stop();
                            ex.printStackTrace();
                        }
                    }

                    public void doFinished() {
                    }
                }.start();
            }
        });

23 楼

二、涉及的进度条加载的程序(一)
22 楼 涉及的类GuageGUI


package com.realty.relation.gui;

import java.awt.*;
import javax.swing.*;

import com.realty.relation.util.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public abstract class GuageGUI {
    private JLabel label;
    private JDialog dialog;
    private Thread thread;
    public GuageGUI(String info) {
        label = new JLabel(info);
        this.buildUI();
    }

    private void finished() {
        doFinished();
        dialog.setVisible(false);
    }

    public abstract void doFinished();

    public abstract void doWork();

    private void buildUI() {
        dialog = lfang.util.SwingUtility.createDialogOnCurrentComponent("", true);
        dialog.setUndecorated(true);
        JPanel pane = new JPanel(new VerticalFlowLayout(true, false));
        JPanel zpane = SwingUtil.createBorderLayoutPane("进度", null);
        pane.add(label);
        JProgressBar bar = new JProgressBar(1, 100);
        bar.setOpaque(false);
        bar.setIndeterminate(true);
        pane.add(bar);

        pane.setOpaque(false);
        zpane.setBackground(new Color(237, 242, 252));

        zpane.add(pane, BorderLayout.CENTER);
        dialog.setContentPane(zpane);
        dialog.setSize(250, 80);
        SwingUtil.centralizeComponent(dialog);
    }

    public void start() {
        thread = new Thread() {
            public void run() {
                try {
                    doWork();
                    Runnable a_doFinished = new Runnable() {
                        public void run() {
                            finished();
                        }
                    };
                    SwingUtilities.invokeLater(a_doFinished);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    SwingUtil.showMessage("发生错误,线程终止");
                    GuageGUI.this.stop();
                } finally {
                    dialog.setVisible(false);
                }
            }
        };
//        dialog.setVisible(true);
        thread.start();
        dialog.setVisible(true);
    }

    public void stop() {
        dialog.setVisible(false);
        thread.interrupt();
        thread = null;
    }

}

24 楼

涉及的进度条加载的程序(二)

23 楼程序涉及的SwingUtil类:



package com.realty.relation.util;

import java.awt.*;
import javax.swing.*;
import javax.swing.FocusManager;
import javax.swing.border.*;


import com.realty.relation.gui.*;


public final class SwingUtil {

    private static Border emptyBorder = new EmptyBorder(2, 2, 2, 2);
    private static Border border = BorderFactory.createCompoundBorder(new ShadowBorder(), emptyBorder);
    private static Color bg = new Color(211, 229, 250);
    private static Color textColor = new Color(10, 36, 106);


    public static void showMessage(String message) {
        JOptionPane.showMessageDialog(javax.swing.FocusManager.getCurrentManager().
                                      getActiveWindow(), message, "消息窗口",
                                      JOptionPane.INFORMATION_MESSAGE);

    }


    public static void centralizeComponent(Component c) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        c.setLocation( (d.width - c.getSize().width) / 2, (d.height - c.getSize().height) / 2);
    }


    public static JPanel createBorderLayoutPane(String title, Icon icon) {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(new TitlePane(title, icon), BorderLayout.NORTH);
        pane.setBorder(new ShadowBorder());
        return pane;
    }



}

25 楼

涉及的进度条加载的程序(三)

package com.realty.relation.gui;

import java.awt.*;
import javax.swing.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class TitlePane extends JPanel {
    private Color GRAD_MID = Color.white;
    private Color GRAD_START = new Color(154, 204, 255);

    public TitlePane(String title, Icon icon) {
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel label = (icon != null) ? new JLabel(title, icon, JLabel.LEADING) : new JLabel(title);
        label.setForeground(Color.black);
        label.setFont(label.getFont().deriveFont(Font.PLAIN));
        this.add(label);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Insets in = getInsets();
        int y = in.top + 13;
        int farRight = getWidth() - in.right;
        int w = farRight - in.left;
        GradientPaint firstHalf = new GradientPaint(in.left, y, GRAD_START, w, y, GRAD_MID);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(firstHalf);
        g2.fillRect(in.left, in.top, w, 25);
    }

}

26 楼

要做一个根据数据量加载的程序要由22楼~25楼的程序拼接而成就可以做作出一个完美的进度条。
 
看的懂的朋友们可以自己研究研究!

涉及的类:GuageGUI.java\SwingUtil.java\TitlePane .java

然后在要用进度条的程序里加载GuageGUI就可以了,如22楼的例子。

我来回复

您尚未登录,请登录后再回复。点此登录或注册