线程不更新GlassPane上的进度条 [英] Thread not Updating Progress Bar on GlassPane
问题描述
在下面的应用程序中,我放置了一个按钮,单击它可以使GlassPane可见,并启动一个线程来更新进度栏的值。下面是代码: - $ /
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;
导入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);
//开始线程更新进度栏
t = new Thread();
t.start();
}});
add(btn);
setVisible(true);
public void run(){
for(int i = 1; i <= 100; i ++)
{
jp.setProgress(i);
尝试{
Thread.sleep(100);
} catch(InterruptedException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}
t = null;
类CustomGlassPane扩展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;
重绘(100,100,200,20);
// repaint();
}
}
但是,虽然GlassPane可见但ProgressBar没有更新。
需要帮助的朋友。
这是一个经典的错误:您正在阻止EDT(Event Dispatching Thread)循环遍历 Thread.sleep()
。 EDT分派所有GUI事件:绘画事件,鼠标事件,按键事件,动作事件等...由于您在EDT中循环和休眠(在ActionEvent期间),它会阻止显示刷新(我下注点击按钮后,图形用户界面变得没有响应)。所以
sleep()
, SwingWorker
或 javax.swing.Timer
。这也可以与传统的 Thread
的和 Executors
(线程池)一起工作,但是你必须确保所有的您尝试修改GUI在EDT中执行。
您可能需要查看这个例子 a>,它显示了如何组合JProgressBar和SwingWorker
In the following application I have put a button ,clicking on which makes the GlassPane visible and a Thread starts which updates the Progress bar value.Below is the code:-
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();
}
}
But,though the GlassPane gets visible but the ProgressBar is not updating. Need Help friends.
This is a classical mistake: you are blocking the EDT (Event Dispatching Thread) with a loop over a Thread.sleep()
. The EDT dispatches all GUI events: paint-events, mouse-events, key-events, action-events etc... Since you are looping and sleeping in the EDT (during an ActionEvent), it prevents the display from refreshing (I bet that your GUI becomes unresponsive after you click the button). So
- Rule #1: do not block the EDT
- Rule #2: DO NOT BLOCK THE EDT, which leads directly to
- Rule #3: do not
sleep()
in the EDT, and - Rule #4: do not perform any lengthy-operation in the EDT
To work around this problem, use SwingWorker
or javax.swing.Timer
. This can also work with traditional Thread
's and Executors
(thread-pools) but you have to make sure that all your attempts to modify the GUI are performed in the EDT.
You may want to have a look at this example which shows how to combine a JProgressBar and a SwingWorker
这篇关于线程不更新GlassPane上的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!