Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JProgressBar没有';t更新,can';我找不到线索_Java_Multithreading_Oracle_Swing_Jprogressbar - Fatal编程技术网

Java JProgressBar没有';t更新,can';我找不到线索

Java JProgressBar没有';t更新,can';我找不到线索,java,multithreading,oracle,swing,jprogressbar,Java,Multithreading,Oracle,Swing,Jprogressbar,干得好,现在我只想知道为什么我在下面的指令System.out.println中添加while循环,Gui中的cmd和Pgbar都会显示进度??: while(progress < 99){ System.out.println("into while of PBar Thread progress = "+progress); if(progress != Path.operationProgress){ operationProgressBar.setValue(

干得好,现在我只想知道为什么我在下面的指令System.out.println中添加while循环,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(); } }
{
线程sa=新线程(){ @凌驾 公开募捐{ 结果=p.模拟退火(邻域型); String lastCostString=result.Cost()+“”; lastCostLabel.setText(lastCostString); }}; sa.start(); Pbar pb=新的Pbar(操作进度条); pb.start(); } //其他一些东西。。。 }


如果您不能使用
SwingWorker
,请使用
SwingUtilities.invokeLater
,例如:

if (progress != Path.operationProgress) {
    final int progressCopy = progress; // Probably not final so copy is needed
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        void run() {
            operationsProgressBar.setValue(progressCopy);
        }
    });
}
注意:执行此操作时,
run
中使用的所有内容都必须是最终的,或者必须有其他度量来访问变量。这一准则在这方面具有象征意义


您需要在事件调度线程之外对Swing组件执行操作,这是无法解决的。

我将使用PropertyChangeListener使退火进度值成为类的“绑定”属性。如果需要,任何观察者都可以遵循此属性。例如:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

@SuppressWarnings("serial")
public class TspGui2 extends JPanel {
   private static final String ANNEALING_PROGRESS = "Annealing Progress";
   private JProgressBar progBar = new JProgressBar(0, 100);
   private JLabel valueLabel = new JLabel();
   private JButton beginAnnealingBtn = new JButton("Begin Annealing");
   private MyAnnealing myAnnealing = new MyAnnealing(this);

   public TspGui2() {
      beginAnnealingBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            beginAnnealing();
         }
      });
      myAnnealing.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(MyAnnealing.ANNEALING)) {
               // be sure this is done on the EDT
               SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                     int annealedValue = myAnnealing.getAnnealedValue();
                     setValue(annealedValue);
                     if (annealedValue >= MyAnnealing.MAX_ANNEALED_VALUE) {
                        beginAnnealingBtn.setEnabled(true);
                     }
                  }
               });
            }
         }
      });
      progBar.setString(ANNEALING_PROGRESS);
      progBar.setStringPainted(true);

      JPanel northPanel = new JPanel(new GridLayout(1, 0));
      northPanel.add(beginAnnealingBtn);
      northPanel.add(valueLabel);

      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(northPanel);
      add(progBar);
   }

   public void setValue(int value) {
      valueLabel.setText("Value:" + value);
      progBar.setValue(value);
   }

   public void beginAnnealing() {
      beginAnnealingBtn.setEnabled(false);
      setValue(0);
      myAnnealing.reset();
      new Thread(new Runnable() {
         public void run() {
            myAnnealing.beginAnnealing();
         }
      }).start();
   }

   private static void createAndShowGui() {
      TspGui2 mainPanel = new TspGui2();

      JFrame frame = new JFrame("TspGui2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

class MyAnnealing {
   public static final String ANNEALING = "Annealing";
   public  static final int MAX_ANNEALED_VALUE = 100;
   private SwingPropertyChangeSupport propChangeSupport = 
         new SwingPropertyChangeSupport(this);
   private TspGui2 gui;
   private int annealedValue;

   public MyAnnealing(TspGui2 gui) {
      this.gui = gui;
   }

   public void addPropertyChangeListener(
         PropertyChangeListener listener) {
      propChangeSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(
         PropertyChangeListener listener) {
      propChangeSupport.removePropertyChangeListener(listener);
   }

   public void reset() {
      setAnnealedValue(0);
   }

   // simulate some long process...
   public void beginAnnealing() {
      long sleepDelay = 100;
      while (annealedValue < MAX_ANNEALED_VALUE) {
         setAnnealedValue(annealedValue + 1);
         try {
            Thread.sleep(sleepDelay);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

   public int getAnnealedValue() {
      return annealedValue;
   }

   private void setAnnealedValue(int value) {
      final int oldValue = this.annealedValue;
      this.annealedValue = value;
      propChangeSupport.firePropertyChange(ANNEALING, oldValue, annealedValue);
   }   
}
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.beans.PropertyChangeEvent;
导入java.beans.PropertyChangeListener;
导入javax.swing.*;
导入javax.swing.event.SwingPropertyChangeSupport;
@抑制警告(“串行”)
公共类TspGui2扩展了JPanel{
私有静态最终字符串退火\u PROGRESS=“退火进度”;
私有JProgressBar progBar=新的JProgressBar(0100);
私有JLabel valueLabel=新JLabel();
私有JButton beginanealingbtn=新JButton(“开始退火”);
私有MyAnnealing MyAnnealing=新MyAnnealing(this);
公共TspGui2(){
beginanealingbtn.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
beginanealing();
}
});
addPropertyChangeListener(新的PropertyChangeListener(){
@凌驾
公共作废属性更改(属性更改事件evt){
if(evt.getPropertyName().equals(MyAnnealing.ANNEALING)){
//确保这是在EDT上完成的
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
int annealedValue=myAnnealing.getAnnealedValue();
设定值(退火值);
if(退火值>=MyAnnealing.MAX\u退火值){
beginanealingbtn.setEnabled(true);
}
}
});
}
}
});
程序设置管柱(退火进度);
progBar.SetStringPaint(真);
JPanel northPanel=新JPanel(新网格布局(1,0));
northPanel.add(BeginAnalingBTN);
northPanel.add(valueLabel);
setLayout(新的BoxLayout(这是BoxLayout.PAGE_轴));
添加(北面板);
添加(progBar);
}
公共无效设置值(int值){
valueLabel.setText(“值:”+Value);
progBar.setValue(值);
}
公共无效开始清理(){
beginanealingbtn.setEnabled(false);
设定值(0);
myAnnealing.reset();
新线程(newrunnable()){
公开募捐{
myAnnealing.beginanealing();
}
}).start();
}
私有静态void createAndShowGui(){
TspGui2主面板=新的TspGui2();
JFrame=新JFrame(“TspGui2”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(主面板);
frame.pack();
frame.setLocationByPlatform(真);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGui();
}
});
}
}
类My退火{
公共静态最终字符串退火=“退火”;
公共静态最终int最大退火值=100;
私有SwingPropertyChangeSupport propChangeSupport=
新的SwingPropertyChangeSupport(此);
私有TspGui2图形用户界面;
私有内部退火价值;
公共图形用户界面(TspGui2 gui){
this.gui=gui;
}
public void addPropertyChangeListener(
PropertyChangeListener(侦听器){
propChangeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(
PropertyChangeListener(侦听器){
propChangeSupport.removePropertyChangeListener(监听器);
}
公共无效重置(){
设置退火值(0);
}
//模拟一些长过程。。。
公共无效开始清理(){
长睡眠延迟=100;
而(退火值<最大退火值){
设置退火值(退火值+1);
试一试{
睡眠(睡眠延迟);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
}
public int getAnnealedValue(){
返回退火值;
}
私有void setAnnealedValue(int值){
最终int oldValue=此.annealedValue;
此参数为:退火值=数值;
propChangeSupport.firePropertyChange(退火、旧值、退火值);
}   
}

尽管您很小心地尝试在后台线程中执行操作,但您的问题闻起来像是线程问题——或者是正在更新的JProgressBar不是正在显示的。但是,如果没有更多的代码,就很难说了。如果上面的代码是在后台线程上运行的,则必须注意在EDT上调用进度条setValue(…),尽管这不会解决问题,但可能会防止某些间歇性异常的发生。我建议您向我们展示更多的代码,最好是
if (progress != Path.operationProgress) {
    final int progressCopy = progress; // Probably not final so copy is needed
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        void run() {
            operationsProgressBar.setValue(progressCopy);
        }
    });
}
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

@SuppressWarnings("serial")
public class TspGui2 extends JPanel {
   private static final String ANNEALING_PROGRESS = "Annealing Progress";
   private JProgressBar progBar = new JProgressBar(0, 100);
   private JLabel valueLabel = new JLabel();
   private JButton beginAnnealingBtn = new JButton("Begin Annealing");
   private MyAnnealing myAnnealing = new MyAnnealing(this);

   public TspGui2() {
      beginAnnealingBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            beginAnnealing();
         }
      });
      myAnnealing.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(MyAnnealing.ANNEALING)) {
               // be sure this is done on the EDT
               SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                     int annealedValue = myAnnealing.getAnnealedValue();
                     setValue(annealedValue);
                     if (annealedValue >= MyAnnealing.MAX_ANNEALED_VALUE) {
                        beginAnnealingBtn.setEnabled(true);
                     }
                  }
               });
            }
         }
      });
      progBar.setString(ANNEALING_PROGRESS);
      progBar.setStringPainted(true);

      JPanel northPanel = new JPanel(new GridLayout(1, 0));
      northPanel.add(beginAnnealingBtn);
      northPanel.add(valueLabel);

      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(northPanel);
      add(progBar);
   }

   public void setValue(int value) {
      valueLabel.setText("Value:" + value);
      progBar.setValue(value);
   }

   public void beginAnnealing() {
      beginAnnealingBtn.setEnabled(false);
      setValue(0);
      myAnnealing.reset();
      new Thread(new Runnable() {
         public void run() {
            myAnnealing.beginAnnealing();
         }
      }).start();
   }

   private static void createAndShowGui() {
      TspGui2 mainPanel = new TspGui2();

      JFrame frame = new JFrame("TspGui2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

class MyAnnealing {
   public static final String ANNEALING = "Annealing";
   public  static final int MAX_ANNEALED_VALUE = 100;
   private SwingPropertyChangeSupport propChangeSupport = 
         new SwingPropertyChangeSupport(this);
   private TspGui2 gui;
   private int annealedValue;

   public MyAnnealing(TspGui2 gui) {
      this.gui = gui;
   }

   public void addPropertyChangeListener(
         PropertyChangeListener listener) {
      propChangeSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(
         PropertyChangeListener listener) {
      propChangeSupport.removePropertyChangeListener(listener);
   }

   public void reset() {
      setAnnealedValue(0);
   }

   // simulate some long process...
   public void beginAnnealing() {
      long sleepDelay = 100;
      while (annealedValue < MAX_ANNEALED_VALUE) {
         setAnnealedValue(annealedValue + 1);
         try {
            Thread.sleep(sleepDelay);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

   public int getAnnealedValue() {
      return annealedValue;
   }

   private void setAnnealedValue(int value) {
      final int oldValue = this.annealedValue;
      this.annealedValue = value;
      propChangeSupport.firePropertyChange(ANNEALING, oldValue, annealedValue);
   }   
}