简体   繁体   English

Nimbus L&F - 改变进度条的背景颜色

[英]Nimbus L&F - Change Background color of Progress Bar

i'm developing a little GUI application with Java using Netbeans Editor. 我正在使用Netbeans Editor使用Java开发一个小的GUI应用程序。 I've put in a JFrame a simple Progress Bar. 我在JFrame中添加了一个简单的进度条。 I'm developing the project with JDK7 我正在用JDK7开发这个项目

I want to change the background Color from default Orange to a personal one. 我想将背景颜色从默认橙色更改为个人颜色。 I've already tried all the properties for the color changing but when i run the program the color still the same. 我已经尝试了颜色变化的所有属性但是当我运行程序时颜色仍然相同。

I've already tried using ProgressBar1.setBackground(new java.awt.Color(0, 204, 255)); 我已经尝试过使用ProgressBar1.setBackground(new java.awt.Color(0,204,255));

and

UIManager.put("ProgressBar.background", Color.YELLOW);
UIManager.put("ProgressBar.foreground", Color.MAGENTA);
UIManager.put("ProgressBar.selectionBackground", Color.red);
UIManager.put("ProgressBar.selectionForeground", Color.green);

Same result..... The background is always orange 相同的结果.....背景总是橙色

Here the code of my test project 这是我的测试项目的代码

public class Frame extends javax.swing.JFrame
{

    public Frame()
    {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jProgressBar1.setBackground(new java.awt.Color(0, 204, 255));
        jProgressBar1.setValue(75);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    public static void main(String args[])
    {
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Frame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JProgressBar jProgressBar1;
    // End of variables declaration
}

and this is the result when you launch the code 这是启动代码时的结果 进度条
(source: uploadscreenshot.com ) (来源: uploadscreenshot.com

Orange color.... 橙色......

EDIT With the following code 编辑使用以下代码

UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled].backgroundPainter", new FillPainter(Color.MAGENTA)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled].backgroundPainter", new FillPainter(Color.MAGENTA)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Indeterminate].progressPadding", new FillPainter(Color.ORANGE)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled].foregroundPainter", new FillPainter(Color.GREEN)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled].foregroundPainter", new FillPainter(Color.GREEN)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Finished].foregroundPainter", new FillPainter(Color.GREEN)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled+Finished].foregroundPainter", new FillPainter(Color.GREEN)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Disabled+Indeterminate].foregroundPainter", new FillPainter(Color.GREEN)); 
UIManager.getLookAndFeelDefaults().put("ProgressBar[Enabled+Indeterminate].foregroundPainter", new FillPainter(Color.GREEN)); `

after this line 在这一行之后

javax.swing.UIManager.setLookAndFeel(info.getClassName());

I've successfully change the color of background. 我成功地改变了背景的颜色。 But now the color is "plain", there is no gradient like the orange color. 但现在颜色是“平原”,没有像橙色那样的渐变。

进度条
(source: uploadscreenshot.com ) (来源: uploadscreenshot.com

Is it possible to change color with the same effect of the original color? 是否可以改变颜色与原始颜色相同的效果?

  1. maybe ( Color and Font ) issue talking about Nimbus Look and Feel 也许( ColorFont )问题谈论Nimbus Look and Feel

  2. have to check this thread 必须检查这个帖子

  3. value for 的价值

ProgressBar[Disabled+Finished].foregroundPainter ProgressBar[Disabled+Indeterminate].foregroundPainter ProgressBar [Disabled + Finished] .foregroundPainter ProgressBar [Disabled + Indeterminate] .foregroundPainter
ProgressBar[Disabled].backgroundPainter ProgressBar[Disabled].foregroundPainter ProgressBar[Enabled+Finished].foregroundPainter ProgressBar[Enabled+Indeterminate].foregroundPainter ProgressBar[Enabled+Indeterminate].progressPadding ProgressBar[Enabled].backgroundPainter ProgressBar[Enabled].foregroundPainter ProgressBar [Disabled] .backgroundPainter ProgressBar [Disabled] .foregroundPainter ProgressBar [Enabled + Finished] .foregroundPainter ProgressBar [Enabled + Indeterminate] .foregroundPainter ProgressBar [Enabled + Indeterminate] .progressPadding ProgressBar [Enabled] .backgroundPainter ProgressBar [Enabled] .foregroundPainter

. 4. maybe easiest for you will be to change nimbusOrange from the top of Nimbus default s, but this change is everywhere, 4.也许最简单的方法是将nimbusOrangeNimbus default s的顶部更改,但这种变化无处不在,

funny output to the GUI with changed Control Color , for example 例如,使用更改的Control Color向GUI输出有趣的输出

UIManager.getLookAndFeelDefaults().put("nimbusOrange", (new Color(127, 255, 191))); UIManager.getLookAndFeelDefaults()。put(“nimbusOrange”,(new Color(127,255,191)));

. 5. for example 5.例如

在此输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import javax.swing.*;

public class MyPopupWithNimbus {

    public MyPopupWithNimbus() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        JList list = new JList();
        panel.add(list);
        JProgressBar progress = new JProgressBar();
        progress.setStringPainted(true);
        progress.setString("60 pct");
        progress.setValue(60);
        frame.add(panel);
        frame.add(progress, BorderLayout.SOUTH);
        JPopupMenu menu = new JPopupMenu();
        menu.add(new JMenuItem("A"));
        menu.add(new JMenuItem("B"));
        menu.add(new JMenuItem("C"));
        JMenu jmenu = new JMenu("D");
        jmenu.add(new JMenuItem("E"));
        menu.add(jmenu);
        frame.setVisible(true);
        menu.show(frame, 50, 50);
    }

    public static void main(String[] args) {

        try {
            for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(laf.getName())) {
                    UIManager.setLookAndFeel(laf.getClassName());
                    UIManager.getLookAndFeelDefaults().put("nimbusOrange", (new Color(127, 255, 191)));
                    UIManager.getLookAndFeelDefaults().put("PopupMenu[Enabled].backgroundPainter",
                            new FillPainter(Color.ORANGE));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                MyPopupWithNimbus aa = new MyPopupWithNimbus();
            }
        });
    }
}

class FillPainter implements Painter<JComponent> {

    private final Color color;

    FillPainter(Color c) {
        color = c;
    }

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {
        g.setColor(color);
        g.fillRect(0, 0, width - 1, height - 1);
    }
}

This is what I have done in the application I currently develop 这就是我在目前开发的应用程序中所做的

ColorUIResource colorResource = new ColorUIResource(Color.red.darker().darker());
UIManager.put("nimbusOrange",colorResource);

This simply changes the default orange color with something more appealing to the eyes and it changes it everywhere. 这简单地改变了默认的橙色,用更吸引眼球的东西,它随处变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM