我想写这样一个进度条:
这个进度条不是定时的,事务处理时间也不可知。进度条随着我的内部复杂的事务处理,在向前前进。等处理完毕后,进度条到头。顺便说一下,该事务处理不是简单的读取文件之类,涉及到很多东西的。小弟刚开始做GUI,不知道如何做,请高手帮忙

解决方案 »

  1.   

    JDK4 开始的进度条可以设置为 来回跑的,就像 Windows 文件搜索 放大镜一样 不知道还要多久,只是告诉你没有人在偷懒。
      

  2.   

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Random;import javax.swing.*;public class ProgressList extends JPanel implements Scrollable
    {
    public ProgressList() 
    {
    super(null);
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    setBackground(Color.WHITE);
    } public Dimension getPreferredScrollableViewportSize()
    {
    return getPreferredSize();
    } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
    {
    if (getComponentCount() > 0) {
    Component c = getComponent(0);
    return c.getHeight();
    }
    return 0;
    } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
    {
    return orientation == SwingConstants.HORIZONTAL ? visibleRect.width : visibleRect.height;
    } public boolean getScrollableTracksViewportWidth()
    {
    return true;
    } public boolean getScrollableTracksViewportHeight()
    {
    return true;
    } private static class Progress extends JPanel { // private static final ImageIcon CANCEL_BTN_ICON = new ImageIcon(ProgressList.class.getResource("imgs/cancelIcon.png"));
    // private static final ImageIcon TITLE_ICON = new ImageIcon(ProgressList.class.getResource("imgs/titleIcon.png")); private static final Color SELECTION_BG = UIManager.getColor("List.selectionBackground");
    private static final Color SELECTION_FG = UIManager.getColor("List.selectionForeground");
    private static final Color UNSELECTION_BG = new Color(245, 245, 245);
    private static final Color UNSELECTION_FG = UIManager.getColor("List.foreground");
         private JLabel titleIconLabel = new JLabel();
    private JLabel titleLabel = new JLabel();
    private JLabel progressInfoLabel = new JLabel();
    private JButton cancelBtn = new JButton();
    private JProgressBar progressBar = new JProgressBar();
    private boolean selected;

    private Progress(String title, int progress, String processInfo) {
    super(new BorderLayout(5, 0)); titleLabel.setText(title);
    progressBar.setValue(progress);
    progressInfoLabel.setText(processInfo);

    // titleIconLabel.setIcon(TITLE_ICON);
    // cancelBtn.setIcon(CANCEL_BTN_ICON);

    JToolBar cancelBtnPane = new JToolBar();
    cancelBtnPane.setOpaque(false);
    cancelBtnPane.setFloatable(false);
    cancelBtnPane.setRollover(true);
    cancelBtnPane.setLayout(new BoxLayout(cancelBtnPane, BoxLayout.X_AXIS));
    cancelBtnPane.setBorder(null);
    cancelBtnPane.add(cancelBtn);

    cancelBtn.setContentAreaFilled(false);
    cancelBtn.setEnabled(isCancelable());
    cancelBtn.setMargin(new Insets(0,0,0,0));
    cancelBtn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    cancel();
    }
    }); JPanel centerPane = new JPanel(new BorderLayout(0, 2));
    centerPane.setOpaque(false); centerPane.add(titleLabel, BorderLayout.NORTH);
    centerPane.add(progressBar, BorderLayout.CENTER);
    centerPane.add(progressInfoLabel, BorderLayout.SOUTH);
    this.add(titleIconLabel, BorderLayout.WEST);
    this.add(centerPane, BorderLayout.CENTER);
    this.add(cancelBtnPane, BorderLayout.EAST); titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
    titleIconLabel.setVerticalAlignment(JLabel.TOP);

    this.setOpaque(true);
    this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 5)); this.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent e) {
    setSelected(!isSelected());
    }
    });

    setSelected(false);
    }

    public Dimension getMaximumSize()
    {
    Dimension d = getPreferredSize();
    d.width = Integer.MAX_VALUE; return d;
    }

    public void setSelected(boolean b)
    {
    this.selected = b;
    if (b) {
    setBackground(SELECTION_BG);
        titleLabel.setForeground(SELECTION_FG);
        progressInfoLabel.setForeground(SELECTION_FG);
    }
    else {
    setBackground(UNSELECTION_BG);
        titleLabel.setForeground(UNSELECTION_FG);
        progressInfoLabel.setForeground(UNSELECTION_FG);
    }
    }

    public boolean isSelected()
    {
    return selected;
    }

    protected void paintComponent(Graphics g)
    {
    ((Graphics2D)g).setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    GradientPaint gp = new GradientPaint(0, 0, getBackground().brighter(), 0, getHeight(), getBackground());

    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(gp);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    }

    private void removeFromProgressList()
    {
    Container parent = getParent();
    if (parent != null) {
    parent.remove(this);
    parent.validate();
    parent.repaint();
    }
    }

    public int getProgress()
    {
    return progressBar.getValue();
    }

    public void setProgress(final int progress)
    {
    if (SwingUtilities.isEventDispatchThread()) {
    progressBar.setValue(progress);
    if (progress >= 100) {
    completed();
    }
    }
    else {
    SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    progressBar.setValue(progress);
    if (progress >= 100) {
    completed();
    }
    }
    });
    }
    }

    public String getProgressInfo() 
    {
    return progressInfoLabel.getText();
    } public void setProcessInfo(final String progressInfo)
    {
    if (SwingUtilities.isEventDispatchThread()) {
    progressInfoLabel.setText(progressInfo);
    }
    else {
    SwingUtilities.invokeLater(new Runnable()
    {
    public void run()
    {
    progressInfoLabel.setText(progressInfo);
    }
    });
    }
    } protected boolean isCancelable() 
    {
    return true;
    }

    protected void cancel() 
    {
    removeFromProgressList();
    } protected void completed()
    {
    removeFromProgressList();
    }
    } private static int n = 0;

    public static void main(String[] args)
    {
    try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    // UIManager.setLookAndFeel(new PlasticXPLookAndFeel());
    } catch (Exception e) {
    e.printStackTrace();
    }

    final Random random = new Random();
    final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    final ProgressList progressList = new ProgressList(); JButton btn = new JButton("Start");
    btn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    final Progress progress = new Progress("Test Progress - " + (n++), 0, " ");
    progressList.add(progress);
    progressList.revalidate();
    progressList.getParent().repaint();

    new Thread() {
    public void run()
    {
    while (true) {
    try {
    Thread.sleep(2000 + random.nextInt(200));
    } catch (InterruptedException e) {
    }
    int t = progress.getProgress();
    if (t >= 100) {
    return;
    } t += random.nextInt(20);
    t = Math.min(t, 100);
    progress.setProgress(t);
    progress.setProcessInfo("Progress " + t + "% complete, current time is " + sdf.format(new Date()));
    }
    }
    }.start();
    }
    });
    JPanel btnPane = new JPanel();
    btnPane.add(btn);

    JScrollPane sp = new JScrollPane(progressList);
    JFrame f = new JFrame("Progress List Test");
    f.getContentPane().add(sp, BorderLayout.CENTER);
    f.getContentPane().add(btnPane, BorderLayout.SOUTH);
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    }
    }