JProgressBar没有更新,找不到线索
干得好,现在我只想知道为什么如果我添加到 while 循环中 System.out.println 下面的指令,进度会显示在 Gui 中的 cmd 和 Pgbar 上? :
while(progress < 99){
System.out.println("into while of PBar Thread progress = "+progress);
if(progress != Path.operationProgress){
operationProgressBar.setValue(progress);
progress = Path.operationProgress;
operationProgressBar.repaint(); } }
需要一些帮助,我无法更新 JProgressBar,我 无法使用 SwingWorker,我必须在没有它的情况下解决这个问题。变量 Path.operationProgress 是“Path”类中的静态变量 实例,并且它是从另一个线程更新的,所以我认为 PBar 和 Path 实例都在用户的线程中执行,而不是在 美东时间 。这是进度条的代码:
import javax.swing.*; 公共类 Pbar 扩展线程 { JProgressBar操作ProgressBar; 公共Pbar(JProgressBar操作进度条){ this.操作进度条 = 操作进度条; } @覆盖 公共无效运行(){ int 进度 = Path.operationProgress; while(进度 < 99) { if(进度!= Path.操作进度) { 操作ProgressBar.setValue(进度); 进度 = Path.操作进度; 操作ProgressBar.repaint(); }}} }
这是启动线程的操作:
私有javax.swing.JProgressBar操作ProgressBar; 私有javax.swing.JLabel路径图像; 私有 javax.swing.JButton 模拟AnnelingButton; 公共类 TSPGUI 扩展 javax.swing.JFrame { TSPMG tspInstance; 路径p,结果; 字符串文件名 = ""; int neighborHood_Type = 1, i = 0; // ......构造函数和 init() 私人无效模拟AnnelingButtonActionPerformed(java.awt.event.ActionEvent evt)
{
线程 sa = 新线程(){ @覆盖 公共无效运行(){ 结果 = p.SimulatedAnnealing(neighborHood_Type); String lastCostString = result.Cost() + ""; lastCostLabel.setText(lastCostString); }}; sa.start(); Pbar pb = new Pbar(操作进度条); pb.start(); } //其他一些东西... }
nice job , now i just wanna know why if i add into while loop the instruction System.out.println below the progress is shown on both , cmd and Pgbar in the Gui ?? :
while(progress < 99){
System.out.println("into while of PBar Thread progress = "+progress);
if(progress != Path.operationProgress){
operationProgressBar.setValue(progress);
progress = Path.operationProgress;
operationProgressBar.repaint(); } }
need some help around , i can't get the JProgressBar to update, i
can't use SwingWorker, i have to solve this without it . the variable
Path.operationProgress is a static variable from a "Path" class
instance, and it's updated from another thread, so i think the PBar
and Path instances are both executed in user's Threads and not in the
EDT . here is the Code of the progress bar :import javax.swing.*; public class Pbar extends Thread { JProgressBar operationProgressBar; public Pbar(JProgressBar operationProgressBar) { this.operationProgressBar = operationProgressBar; } @Override public void run() { int progress = Path.operationProgress; while(progress < 99) { if(progress != Path.operationProgress) { operationProgressBar.setValue(progress); progress = Path.operationProgress; operationProgressBar.repaint(); }}} }
this is the action that launches the threads :
private javax.swing.JProgressBar operationProgressBar; private javax.swing.JLabel pathImage; private javax.swing.JButton simulatedAnnelingButton; public class TSPGUI extends javax.swing.JFrame { TSPMG tspInstance; Path p, result; String filename = ""; int neighborHood_Type = 1, i = 0; // ......Constructor Stuff and init() private void simulatedAnnelingButtonActionPerformed(java.awt.event.ActionEvent evt)
{
Thread sa = new Thread(){
@Override
public void run(){
result = p.SimulatedAnnealing(neighborHood_Type);
String lastCostString = result.Cost() + "";
lastCostLabel.setText(lastCostString);
}};
sa.start();
Pbar pb = new Pbar(operationProgressBar);
pb.start();
}
//Some other Stuff ...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不能使用
SwingWorker
,则使用SwingUtilities.invokeLater
,例如:注意:执行此操作时,
run
中使用的所有内容都必须是最终的或者必须有其他措施来访问变量。这段代码在这方面具有象征意义。您需要在事件调度线程之外对 Swing 组件进行操作,没有办法解决这个问题。
If you can't use
SwingWorker
then useSwingUtilities.invokeLater
, e.g.:Note: When doing this, everything used in
run
has to be final or there have to be other measures to access the variables. This code is symbolic in that regard.You need to do operations on Swing components outside the event dispatching thread, there is no way around this.
我将使用 PropertyChangeListener 来允许您将退火进度值设置为类的“绑定”属性。如果需要的话,任何观察者都可以遵循这个属性。例如:
I would use a PropertyChangeListener to allow you to make the annealing progress value a "bound" property of the class. Than any observer can follow this property if desired. For example: