JProgressbar:如何根据进度改变颜色? [英] JProgressbar: how to change colour based on progress?

查看:862
本文介绍了JProgressbar:如何根据进度改变颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以根据进度的值更改条形颜色?我尝试以下但它不工作:

Is it possible to be able to change the bar colour depending on the value of the progress? I tried the following but it doesn't work:

percentUsed = (int)(((float) used / (float) max) * BAR_PERCENTAGE);
      if (percentUsed >= ORANGE_THRESHOLD &&  percentUsed < RED_THRESHOLD) {
        if (!m_orangeIndicator) {
          LOG.warn(String.format("Memory usage exceeds %d percent.", ORANGE_THRESHOLD));
          m_orangeIndicator = true;
        }
        colour = Color.ORANGE;
        m_redIndicator = false;
      }
      else if (percentUsed >= RED_THRESHOLD) {
        if (!m_redIndicator) {
          LOG.warn(String.format("Memory usage exceeds %d percent.", RED_THRESHOLD));
          m_orangeIndicator = true;
          m_redIndicator = true;
        }
        colour = Color.RED;
      }
      else {
        m_orangeIndicator = false;
        m_redIndicator = false;
        colour = Color.GREEN;
      }
      m_memUsageBar.setForeground(colour);
      m_memUsageBar.setValue(percentUsed);
      m_memUsageBar.updateUI();

我猜这不是一件小事,因为JProgressbar不是这样使用的

I am guessing it is not a trivial thing to do because JProgressbar is not meant to be used that way... But is it possible or are there alternatives?

推荐答案

感谢您的帮助,但试验和错误我发现一种方式使用我现有的代码来改变颜色。

Thanks for the help but by trial and error I found a way to get the colour changing with using my existing code.

我不知道它的JPrgressbar的怪癖,但如果调用做下面的调用前景栏颜色可以改变动态与最小返工:

I am not sure if its the quirk of JPrgressbar but if call do the following call the foreground bar colour can be changed dynamically with minimal rework:

progressBar.setStringPainted(true);

这篇关于JProgressbar:如何根据进度改变颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆