无法在 Mac OS 外观和感觉中更改 JProgressBar 颜色 [英] Can't change JProgressBar color in Mac OS look and feel

查看:31
本文介绍了无法在 Mac OS 外观和感觉中更改 JProgressBar 颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道之前已经回答过这个问题,但它对我不起作用.我按照这里的说明操作:,如下图

com.apple.laf.AquaLookAndFeel 上,进度条的 UI 委托是 com.apple.laf.AquaProgressBarUI 的一个实例.正如您所发现的,它忽略了许多默认值而支持原生组件.如果需要新颖的配色方案,请考虑提供您自己的 UI 委托,如此处所示.

AquaProgressBarUI:

CustomProgressUI:

ProgressBar UI 默认值:

<前>ProgressBar.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]ProgressBar.border: javax.swing.plaf.BorderUIResource@47f08ed8ProgressBar.cellLength: 1ProgressBar.cellSpacing: 0ProgressBar.cycleTime:3000ProgressBar.font: sun.swing.SwingLazyValue@6446d228ProgressBar.foreground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]ProgressBar.horizo​​ntalSize: javax.swing.plaf.DimensionUIResource[width=146,height=12]ProgressBar.repaintInterval: 20ProgressBar.selectionBackground: javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]ProgressBar.selectionForeground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]ProgressBar.verticalSize: javax.swing.plaf.DimensionUIResource[width=12,height=146]ProgressBarUI:com.apple.laf.AquaProgressBarUI

SSCCE:

import java.awt.*;导入 javax.swing.*;公共类 ProgressBarTest 扩展 JFrame {公共静态无效主(字符串参数[]){UIManager.put("ProgressBar.repaintInterval", 100);UIManager.put("ProgressBar.border",BorderFactory.createLineBorder(Color.blue, 2));EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){JFrame f = new JFrame();f.setLayout(new GridLayout(0, 1, 5, 5));f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.add(createBar());f.add(createBar());f.add(createBar());f.pack();f.setLocationRelativeTo(null);f.setVisible(true);}私人 JProgressBar createBar() {JProgressBar progressBar = new JProgressBar(0, 100);progressBar.setValue(50);返回进度条;}});}}

I know this question has been answered before, but it's just not working for me. I followed the instructions from here: How to change JProgressBar color?

import javax.swing.*;
import java.awt.*;

public class ProgressBarTest extends JFrame {

    public static void main(String args[]) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        UIManager.put("ProgressBar.background", Color.orange);
        UIManager.put("ProgressBar.foreground", Color.black);
        UIManager.put("ProgressBar.selectionBackground", Color.red);
        UIManager.put("ProgressBar.selectionForeground", Color.green);
        JProgressBar progressBar = new JProgressBar(0,100);
        progressBar.setValue(50);
        f.add(progressBar, BorderLayout.PAGE_END);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}

All I am getting is the same old colors.

I'm using Mac OS X 10.7.3 and Java 1.6. I tried the CrossPlatformLookAndFeel and it works with the new colors. However I want this in the default look and feel. How can I do this?

解决方案

To override Look & Feel defaults, make the change before constructing the GUI on the event dispatch thread, as shown below.

On the com.apple.laf.AquaLookAndFeel, the progress bar's UI delegate is an instance of com.apple.laf.AquaProgressBarUI. As you have found, it ignores many defaults in favor of the native component. If a novel color scheme is required, consider supplying your own UI delegate, as shown here.

AquaProgressBarUI:

CustomProgressUI:

ProgressBar UI Defaults:

ProgressBar.background: com.apple.laf.AquaNativeResources$CColorPaintUIResource[r=238,g=238,b=238]
ProgressBar.border: javax.swing.plaf.BorderUIResource@47f08ed8
ProgressBar.cellLength: 1
ProgressBar.cellSpacing: 0
ProgressBar.cycleTime: 3000
ProgressBar.font: sun.swing.SwingLazyValue@6446d228
ProgressBar.foreground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.horizontalSize: javax.swing.plaf.DimensionUIResource[width=146,height=12]
ProgressBar.repaintInterval: 20
ProgressBar.selectionBackground: javax.swing.plaf.ColorUIResource[r=255,g=255,b=255]
ProgressBar.selectionForeground: javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
ProgressBar.verticalSize: javax.swing.plaf.DimensionUIResource[width=12,height=146]
ProgressBarUI: com.apple.laf.AquaProgressBarUI

SSCCE:

import java.awt.*;
import javax.swing.*;

public class ProgressBarTest extends JFrame {

    public static void main(String args[]) {
        UIManager.put("ProgressBar.repaintInterval", 100);
        UIManager.put("ProgressBar.border",
            BorderFactory.createLineBorder(Color.blue, 2));
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setLayout(new GridLayout(0, 1, 5 , 5));
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(createBar());
                f.add(createBar());
                f.add(createBar());
                f.pack();
                f.setLocationRelativeTo(null);
                f.setVisible(true);
            }

            private JProgressBar createBar() {
                JProgressBar progressBar = new JProgressBar(0, 100);
                progressBar.setValue(50);
                return progressBar;
            }
        });
    }
}

这篇关于无法在 Mac OS 外观和感觉中更改 JProgressBar 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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