JProgressBar百分比不同进度条颜色不同

jopen 12年前

1、需求

需要做一个JProgressBar,1/3以下时为红色,2/3以下时为黄色,以上为绿色有一点像预警系统

2、代码实现GyrJProgressBar

 GyrJProgressBar中利用自定义的ProgressBarUI来改变进度条的颜色

package org.noahx.ui.progressbar;    import javax.swing.*;  import javax.swing.plaf.basic.BasicProgressBarUI;  import java.awt.*;    /**   * Created with IntelliJ IDEA.   * User: noah   * Date: 7/24/12   * Time: 8:43 PM   * To change this template use File | Settings | File Templates.   */  public class GyrJProgressBar extends JProgressBar {        private class GyrProgressUI extends BasicProgressBarUI {            private double greenOverPercent=(2d/3d)*100d;            private double yellowOverPercent=(1d/3d)*100d;            private JProgressBar jProgressBar;            private GyrProgressUI(JProgressBar jProgressBar) {              this.jProgressBar = jProgressBar;          }            @Override          protected void paintDeterminate(Graphics g, JComponent c) {                  double percent=100d*this.jProgressBar.getValue()/(this.jProgressBar.getMaximum()-this.jProgressBar.getMinimum());                if (percent > this.greenOverPercent) {                  this.jProgressBar.setForeground(Color.green);              } else if (percent > this.yellowOverPercent) {                  this.jProgressBar.setForeground(Color.yellow);              } else {                  this.jProgressBar.setForeground(Color.red);              }              super.paintDeterminate(g, c);          }        }        public GyrJProgressBar() {          init();      }        public GyrJProgressBar(int orient) {          super(orient);          init();      }        public GyrJProgressBar(int min, int max) {          super(min, max);          init();      }        public GyrJProgressBar(int orient, int min, int max) {          super(orient, min, max);          init();      }        public GyrJProgressBar(BoundedRangeModel newModel) {          super(newModel);          init();      }        private void init(){          this.setBorderPainted(false);          this.setUI(new GyrProgressUI(this));      }  }

3、效果

JProgressBar百分比不同进度条颜色不同