如何改变JProgressBar的颜色

发布于 2025-01-19 02:32:44 字数 572 浏览 3 评论 0原文

1我试图更改进度条的颜色,但它保持橙色,这是代码:

test(){  
        UIManager.put("ProgressBar.background", Color.BLACK);
        UIManager.put("ProgressBar.foreground", Color.RED);
        UIManager.put("ProgressBar.selectionBackground", Color.YELLOW);
        UIManager.put("ProgressBar.selectionForeground", Color.BLUE);
        
          
         bar = new JProgressBar();
         bar.setBounds(20, 30,400 , 200);
         bar.setString("Welcome...");
         bar.setStringPainted(true);
         this.add(bar);

        

我尝试了.setBackground,它也无法正常工作

1 I am trying to change the color of the progress bar but it stays orange here's the code:

test(){  
        UIManager.put("ProgressBar.background", Color.BLACK);
        UIManager.put("ProgressBar.foreground", Color.RED);
        UIManager.put("ProgressBar.selectionBackground", Color.YELLOW);
        UIManager.put("ProgressBar.selectionForeground", Color.BLUE);
        
          
         bar = new JProgressBar();
         bar.setBounds(20, 30,400 , 200);
         bar.setString("Welcome...");
         bar.setStringPainted(true);
         this.add(bar);

        

I've tried .setbackground it also didnt work

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

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

发布评论

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

评论(1

千纸鹤 2025-01-26 02:32:46

您的代码问题可能与您调用 UIManager 的位置有关。
如果在对象初始化之前调用它,它会正常工作:

UIManager.put("ProgressBar.background", Color.GREEN);
UIManager.put("ProgressBar.foreground", Color.BLUE);
UIManager.put("ProgressBar.selectionBackground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.GREEN);

JProgressBar progress = new JProgressBar();

结果:
输入图片此处描述

,如果您在初始化后调用 UIManager,结果将会有所不同:

JProgressBar progress = new JProgressBar();

UIManager.put("ProgressBar.background", Color.GREEN);
UIManager.put("ProgressBar.foreground", Color.BLUE);
UIManager.put("ProgressBar.selectionBackground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.GREEN);

结果:
在此处输入图像描述]2

the problem with your code might relate to the location which you called UIManager.
if you call it before object initialization it works correctly:

UIManager.put("ProgressBar.background", Color.GREEN);
UIManager.put("ProgressBar.foreground", Color.BLUE);
UIManager.put("ProgressBar.selectionBackground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.GREEN);

JProgressBar progress = new JProgressBar();

result:
enter image description here

and if you call UIManager after initialization the result will differ:

JProgressBar progress = new JProgressBar();

UIManager.put("ProgressBar.background", Color.GREEN);
UIManager.put("ProgressBar.foreground", Color.BLUE);
UIManager.put("ProgressBar.selectionBackground", Color.RED);
UIManager.put("ProgressBar.selectionForeground", Color.GREEN);

result:
enter image description here]2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文