线程不在GlassPane上更新进度条

时间:2013-01-03 12:49:25

标签: java swing graphics graphics2d

在下面的应用程序中,我放了一个按钮,单击它使GlassPane可见,并启动一个Thread来更新Progress bar值。下面是代码: -

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LinearGradientPaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;


public class GlassPaneDownload extends JFrame implements Runnable{
Thread t;
CustomGlassPane jp;

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

public GlassPaneDownload(){
super("Glass Pane Download Simulation");
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());    

jp=new CustomGlassPane();

jp.setOpaque(false);
setGlassPane(jp);


JButton btn=new JButton("Click Here");
btn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){
    //Make Glass Pane visible
    jp.setVisible(true);
    //Start Thread to update Progress Bar
    t=new Thread();
    t.start();
}});
add(btn);
setVisible(true);
}
public void run(){
for(int i=1;i<=100;i++)
{
    jp.setProgress(i);
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
t=null;
}
}

class CustomGlassPane extends JComponent
{


    private final float pattern[]=new float[]{0.0f,0.499f,0.50f,1.0f};
    private final Color color[]=new Color[]{Color.WHITE,Color.GRAY,Color.BLACK,Color.LIGHT_GRAY};
    private int progress,oldProgress;


    public void paintComponent(Graphics g1)
    {
        super.paintComponent(g1);
        g1.setColor(new Color(1.0f,1.0f,1.0f,0.7f));
        g1.fillRect(0, 0, getWidth(), getHeight());
        g1.setColor(new Color(200,200,255));
        g1.drawRect(100,100,200,20);

        LinearGradientPaint p=new LinearGradientPaint(100,100,200,20,pattern,color);
        Graphics2D g2=(Graphics2D)g1;
        g2.setPaint(p);
        g2.fillRect(100, 100,progress*2, 20);
    }
    public void setProgress(int prog)
    {
        progress=prog;
        repaint(100,100,200,20);
        //repaint();
    }

}

但是,虽然GlassPane可见,但ProgressBar没有更新。 需要帮助的朋友。

1 个答案:

答案 0 :(得分:2)

这是一个经典错误:您通过Thread.sleep()上的循环阻止了EDT(事件调度线程)。 EDT调度所有GUI事件:绘制事件,鼠标事件,键事件,动作事件等...由于您在EDT中循环和休眠(在ActionEvent期间),它会阻止显示刷新(我打赌)单击按钮后,您的GUI将无响应)。所以

  • 规则#1:不要阻止EDT
  • 规则#2:不要阻止EDT,直接导致
  • 规则#3:在EDT中不要sleep()
  • 规则#4:不要在EDT中执行任何冗长的操作

要解决此问题,请使用SwingWorkerjavax.swing.Timer。这也适用于传统的ThreadExecutors(线程池),但您必须确保所有修改GUI的尝试都在EDT中执行。

您可能需要查看this example,其中显示了如何组合JProgressBar和SwingWorker