为什么Java Swing JProgressBar在Nimbu Look and Feel中无法正常工作?

时间:2014-04-12 17:25:01

标签: java swing progress-bar look-and-feel nimbus

我正在开发一个简单的java项目,它遍历指定的目录并找到冗余文件。除了一件事以外的一切都工作正常,我使用JProgressBar来显示目录跨越的状态。它与其他外观和感觉一起工作得很好但是,当我设置我最喜欢的“Nimbus Look& Feel”时,进度条没有显示进度的填充颜色。我设置进度条来绘制字符串。字符串显示(3%,5%......)。我是新手,在java中看起来和感觉。

代码我用来更新进度条......

private synchronized void updateProgress(long length)
{
        prog = prog + (length/onePart);

        if(prog>100)
            prog = 100;

        addLog((int)prog + " % completed.");

        SwingUtilities.invokeLater(new Runnable() 
        {
            @Override
            public void run() {
                progress.setValue((int)prog);
                progress.setString((int)prog+" %");
                progress.update(progress.getGraphics());
            }
        });
}

enter image description here

1 个答案:

答案 0 :(得分:3)

我再次建议您像下面的代码一样创建minimal example program。实际上,请随意修改此代码,以便它能够重现您的问题:

import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

@SuppressWarnings("serial")
public class NimbusFoo extends JPanel {
   private JProgressBar progressBar = new JProgressBar();

   public NimbusFoo() {
      progressBar.setStringPainted(true);
      add(progressBar);
      add(new JButton(new StartAction("Start", KeyEvent.VK_S)));
   }

   private class StartAction extends AbstractAction {
      public StartAction(String name, int mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      public void actionPerformed(java.awt.event.ActionEvent evt) {
         final JButton btn = (JButton) evt.getSource();
         btn.setEnabled(false);

         final SwingWorker<Void, Void> mySwingWorker = new SwingWorker<Void, Void>() {
            Random random = new Random();
            private long sleepTime = 200;
            protected Void doInBackground() throws Exception {
               int progress = 0;
               setProgress(progress);

               while (progress < 100) {
                  progress += random.nextInt(5);
                  progress = Math.min(100, progress);
                  setProgress(progress);
                  Thread.sleep(sleepTime);
               }
               return null;
            };
         };
         mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent pcEvt) {
               if ("progress".equals(pcEvt.getPropertyName())) {
                  progressBar.setValue(mySwingWorker.getProgress());
                  progressBar.setString(mySwingWorker.getProgress() + "%");
               }
               if ("state".equals(pcEvt.getPropertyName())) {
                  if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
                     btn.setEnabled(true);
                  }
               }
            }
         });
         mySwingWorker.execute();
      };
   }

   private static void createAndShowGui() {

      try {
         for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
               UIManager.setLookAndFeel(info.getClassName());
               break;
            }
         }
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      } catch (InstantiationException e) {
         e.printStackTrace();
      } catch (IllegalAccessException e) {
         e.printStackTrace();
      } catch (UnsupportedLookAndFeelException e) {
         e.printStackTrace();
      }

      NimbusFoo mainPanel = new NimbusFoo();

      JFrame frame = new JFrame("Nimbus Fun");
      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();
         }
      });
   }
}

enter image description here