设置JProgressBar文本的颜色

8

我有这些进度条:

示例进度条

我想要设置进度条文本的颜色,而不改变背景颜色或进度条本身的颜色。据我所知,setForeground()会同时设置文本和进度条颜色?有没有一种方法可以单独实现其中一个?

2个回答

21

啊哈 - 看起来我可以修改用户界面:

    setUI(new BasicProgressBarUI() {
      protected Color getSelectionBackground() { return Color.black; }
      protected Color getSelectionForeground() { return Color.white; }
    });
这有点令人困惑,因为前景和背景的使用并不直观。 背景是当栏不覆盖文本时文本的颜色,前景是覆盖时文本的颜色。
这是最终结果: 已调整颜色

4
通过UIDefaults的消息,
ProgressBar.background is the background color
ProgressBar.foreground is the cell color
ProgressBar.selectionBackground is the text color outside cells
ProgressBar.selectionForeground is the text colot INSIDE cells

UIManager.put("ProgressBar.selectionForeground", Color.black);
UIManager.put("ProgressBar.selectionBackground", Color.black);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接