简体   繁体   English

为什么Java Swing JProgressBar在Nimbu外观中无法正常工作?

[英]Why Java Swing JProgressBar is not working properly in Nimbu Look and Feel?

I am working on one simple java project which traverses through the specified directory and finds the redundant files. 我正在研究一个简单的Java项目,该项目遍历指定目录并找到冗余文件。 Everything except one thing is working fine, I am using JProgressBar to show the status of directory spanning. 除了一件事情,其他所有东西都工作正常,我正在使用JProgressBar来显示目录跨越的状态。 It is working just fine with other look and feels but, when I set my favourite "Nimbus Look & Feel", the progress bar is not showing the fill color of progress. 它可以与其他外观完美配合,但是当我设置我最喜欢的“ Nimbus外观”时,进度条没有显示进度的填充颜色。 I set the progress bar to paint the string along. 我设置进度条来绘制字符串。 The string is showing up(3%, 5%...). 字符串显示出来(3%,5%...)。 I am new to look and feels in java. 我是Java的新手。

Code I used to update the progress bar... 我用来更新进度条的代码...

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());
            }
        });
}

在此处输入图片说明

I again suggest you create a minimal example program like the code below. 我再次建议您创建一个最小的示例程序,如下面的代码。 In fact, please feel free to modify this code so that it reproduces your problem: 实际上,请随时修改此代码,以便重现您的问题:

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();
         }
      });
   }
}

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM