2

我知道这个问题以前已经回答过,但它对我不起作用。我按照这里的说明进行操作:如何更改 JProgressBar 颜色?

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);
    }

}

我得到的只是相同的旧颜色。

AquaProgressBarUI

我正在使用 Mac OS X 10.7.3 和 Java 1.6。我试过了CrossPlatformLookAndFeel,它适用于新颜色。但是,我希望它具有默认的外观和感觉。我怎样才能做到这一点?

4

1 回答 1

4

要覆盖 Look & Feel 默认值,请在事件调度线程上构建 GUI之前进行更改,如下所示。

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

AquaProgressBarUI

AquaProgressBarUI

CustomProgressUI:

CustomProgressUI

ProgressBar UI 默认值:

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.horizo​​ntalSize: 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;
            }
        });
    }
}
于 2012-06-22T00:46:08.810 回答