使用 Nimbus 更改 JProgressBar 的颜色?

发布于 2024-12-01 01:41:59 字数 58 浏览 1 评论 0原文

有谁知道当您使用 Nimbus LookAndFeel 时如何更改 JProgressBar 的颜色?

does anyone know how to change the colors for JProgressBar when you use Nimbus LookAndFeel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2024-12-08 01:41:59

我已经覆盖了整个 nimbusOrange-默认值,它更改了所有 ProgressBar-Colors 和任何其他 nimbusOrange(InternalFrame - 最小化按钮)

这里使用 nimbusBase (蓝色)

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusOrange",defaults.get("nimbusBase"));

更好的是编写一个自己的 Painter 并将其设置为 UIManager 通过

UIManager.put("ProgressBar[Enabled].backgroundPainter", myPainter);

如果您只想更改单个 ProgressBar 实例的颜色,您可以使用每个组件自定义

progress = new JProgressBar();
UIDefaults defaults = new UIDefaults();
defaults.put("ProgressBar[Enabled].backgroundPainter", new MyPainter());
progress.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
progress.putClientProperty("Nimbus.Overrides", defaults);

I have overridden the whole nimbusOrange-Default Value, which change all ProgressBar-Colors and any other nimbusOrange. (InternalFrame - minimize Button)

here with nimbusBase (blue)

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.put("nimbusOrange",defaults.get("nimbusBase"));

Better is to write a own Painter and set this to the UIManager via

UIManager.put("ProgressBar[Enabled].backgroundPainter", myPainter);

If You want to change the Color for only a single ProgressBar instance, you can use Per-component customization

progress = new JProgressBar();
UIDefaults defaults = new UIDefaults();
defaults.put("ProgressBar[Enabled].backgroundPainter", new MyPainter());
progress.putClientProperty("Nimbus.Overrides.InheritDefaults", Boolean.TRUE);
progress.putClientProperty("Nimbus.Overrides", defaults);
北城挽邺 2024-12-08 01:41:59

MyPainter 的一个例子如下:

class MyPainter implements Painter<JProgressBar> {

    private final Color color;

    public MyPainter(Color c1) {
        this.color = c1;
    }
    @Override
    public void paint(Graphics2D gd, JProgressBar t, int width, int height) {
        gd.setColor(color);
        gd.fillRect(0, 0, width, height);
    }
}

但是我的编译器或 IDE (eclipse) 说它不知道 Painter。
有没有人可以帮助我!

an example of MyPainter can be as following:

class MyPainter implements Painter<JProgressBar> {

    private final Color color;

    public MyPainter(Color c1) {
        this.color = c1;
    }
    @Override
    public void paint(Graphics2D gd, JProgressBar t, int width, int height) {
        gd.setColor(color);
        gd.fillRect(0, 0, width, height);
    }
}

but my compiler or IDE (eclipse) says that it doesn't know the Painter.
is there anybody to help me!

全部不再 2024-12-08 01:41:59
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JProgressBarDemo extends JFrame {
    protected int minValue = 0;
    protected int maxValue = 100;
    protected int counter = 0;
    protected JProgressBar progressBar;

    public JProgressBarDemo() {
        super("JProgressBar Demo");
        setSize(300, 100);

        UIManager.put("ProgressBar.background", Color.BLACK); //colour of the background
        UIManager.put("ProgressBar.foreground", Color.RED); //colour of progress bar
        UIManager.put("ProgressBar.selectionBackground",Color.YELLOW); //colour of percentage counter on black background
        UIManager.put("ProgressBar.selectionForeground",Color.BLUE); //colour of precentage counter on red background

        progressBar = new JProgressBar();
        progressBar.setMinimum(minValue);
        progressBar.setMaximum(maxValue);
        progressBar.setStringPainted(true);
        JButton start = new JButton("Start");

        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Thread runner = new Thread() {
                    public void run() {
                        counter = minValue;
                        while (counter <= maxValue) {
                            Runnable runme = new Runnable() {
                                public void run() {
                                    progressBar.setValue(counter);
                                }
                            };

                            SwingUtilities.invokeLater(runme);
                            counter++;
                            try {
                                Thread.sleep(100);
                            } catch (Exception ex) {
                            }
                        }
                    }
                };
                runner.start();
            }
        });

        getContentPane().add(progressBar, BorderLayout.CENTER);
        getContentPane().add(start, BorderLayout.WEST);
        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };

        addWindowListener(wndCloser);
        setVisible(true);
    }
    public static void main(String[] args) {
        new JProgressBarDemo();
    }
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JProgressBarDemo extends JFrame {
    protected int minValue = 0;
    protected int maxValue = 100;
    protected int counter = 0;
    protected JProgressBar progressBar;

    public JProgressBarDemo() {
        super("JProgressBar Demo");
        setSize(300, 100);

        UIManager.put("ProgressBar.background", Color.BLACK); //colour of the background
        UIManager.put("ProgressBar.foreground", Color.RED); //colour of progress bar
        UIManager.put("ProgressBar.selectionBackground",Color.YELLOW); //colour of percentage counter on black background
        UIManager.put("ProgressBar.selectionForeground",Color.BLUE); //colour of precentage counter on red background

        progressBar = new JProgressBar();
        progressBar.setMinimum(minValue);
        progressBar.setMaximum(maxValue);
        progressBar.setStringPainted(true);
        JButton start = new JButton("Start");

        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Thread runner = new Thread() {
                    public void run() {
                        counter = minValue;
                        while (counter <= maxValue) {
                            Runnable runme = new Runnable() {
                                public void run() {
                                    progressBar.setValue(counter);
                                }
                            };

                            SwingUtilities.invokeLater(runme);
                            counter++;
                            try {
                                Thread.sleep(100);
                            } catch (Exception ex) {
                            }
                        }
                    }
                };
                runner.start();
            }
        });

        getContentPane().add(progressBar, BorderLayout.CENTER);
        getContentPane().add(start, BorderLayout.WEST);
        WindowListener wndCloser = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        };

        addWindowListener(wndCloser);
        setVisible(true);
    }
    public static void main(String[] args) {
        new JProgressBarDemo();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文