我有一个名为playerhealth的JProgressBar。我把条子的颜色改成了绿色。这使得文本很难看到,所以我想将JProgressBar中文本的颜色设置为黑色。
我读到您可以使用UIManager来设置JProgressBars的全局颜色,但我只想为这个(和另一个,但这无关紧要)这样做。
我还读到了HERE,我唯一的其他选择就是修改JProgressBar类。我该怎么做呢?
发布于 2015-02-04 23:04:53
看一下源代码,这并不容易。
颜色存储在BasicProgressBarUI类中:
UIManager中检索颜色。静态方法意味着您不能覆盖调用。用于JProgressBar的ProgressBarUI实例派生自UIManager (UIManager#getUI),这也是一个静态方法。
这就给我们留下了不多的选择。我认为一个可行的方法是使用JProgressBar#setUI方法:
这允许您创建自己的UI instance
这种方法的主要缺点是,它要求您预先知道您的应用程序将使用的外观。例如,如果应用程序使用Metal,这将变成
JProgressBar progressBar = ... ;
ProgressBarUI ui = new MetalProgressBarUI(){
/**
* The "selectionForeground" is the color of the text when it is painted
* over a filled area of the progress bar.
*/
@Override
protected Color getSelectionForeground() {
//return your custom color here
}
/**
* The "selectionBackground" is the color of the text when it is painted
* over an unfilled area of the progress bar.
*/
@Override
protected Color getSelectionBackground()
//return your custom color here
}
}
progressBar.setUI( ui );由于必须预先了解外观的主要缺点,对此解决方案不是100%满意。
发布于 2015-02-04 22:52:34
您可以将setStringPainted属性设置为true:
progressBar.setStringPainted(true);
progressBar.setForeground(Color.blue);
progressBar.setString("10%");https://stackoverflow.com/questions/28324155
复制相似问题