繁体   English   中英

使用nimbus定制JComboBox

[英]Customizing JComboBox with nimbus

我正在尝试使用nimbus L&F自定义JComboBox的外观。

这是一些代码:

命名为Painter.java

package gui.combo;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.Painter;

public class NamedPainter implements Painter<JComponent>
{
    String name;

    public NamedPainter(String name)
    {
        this.name = name;
    }

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height)
    {
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Arial", Font.PLAIN, 12));
        g.setColor(Color.YELLOW);
        g.drawString(name, 0, 10);
    }
}

ColorRectanglePainter.java

package gui.combo;

import java.awt.Color;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.Painter;

public class ColorRectanglePainter implements Painter<JComponent>
{

    private final Color color;

    public ColorRectanglePainter(final Color color)
    {
        this.color = color;
    }

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

}

CustomizeComboNimbus.java

package gui.combo;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Map.Entry;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class CustomizeComboNimbus
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // Set nimbus L&F
        try
        {
            for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
            {
                if("Nimbus".equals(info.getName()))
                {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }

        catch(Exception e)
        {
            // No nimbus...
            e.printStackTrace();
            return;
        }

        ColorRectanglePainter redPainter = new ColorRectanglePainter(Color.RED);

        // Get default UI and modify it 
        final UIDefaults boxDefaults = new UIDefaults();
        for(Entry<Object, Object> entry : UIManager.getLookAndFeelDefaults().entrySet())
        {
            try
            {
                String key = (String)entry.getKey();
                if(key.startsWith("ComboBox"))
                {
                    if(key.contains("Painter"))
                    {
                        if(key.contains("arrowButton"))
                        {
                            // Set a painter which paint a red rectangle for arrowButton
                            boxDefaults.put(key, redPainter);
                            System.err.println("Replacing the painter for " + key + " with redPainter");
                        }
                        else
                        {
                            // Set a painter that display the name of the nimbusKey when it is triggered
                            NamedPainter painter = new NamedPainter(key);
                            boxDefaults.put(key, painter);
                            System.err.println("Replacing the painter for " + key + " with NamedPainter");
                        }
                    }
                }
            }
            catch(Exception e)
            {
            }
        }

        final String[] toDisplay = { "Hello", "World", "Pimp", "My", "Combo" };

        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                JFrame frame = new JFrame("Pimp my combo");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JComboBox<String> classicCombo = new JComboBox<>(toDisplay);
                JComboBox<String> pimpedCombo = new JComboBox<>(toDisplay);
                // set the modif to pimpedCombo
                pimpedCombo.putClientProperty("Nimbus.Overrides", boxDefaults);
                pimpedCombo.putClientProperty("Nimbus.Overrides.InheritDefaults", false);

                Container pane = frame.getContentPane();
                pane.setLayout(new GridLayout(2, 1));
                pane.add(classicCombo);
                pane.add(pimpedCombo);
                frame.pack();
                frame.setVisible(true);
            }
        });

    }
}

这是控制台输出:

Replacing the painter for ComboBox:"ComboBox.arrowButton" Editable+Selected].backgroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.textField"[Enabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Editable+Focused].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Disabled].foregroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Editable+MouseOver].backgroundPainter with redPainter
Replacing the painter for ComboBox[Enabled+Selected].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[MouseOver].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Selected].foregroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Disabled+Editable].backgroundPainter with redPainter
Replacing the painter for ComboBox[Editable+Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Focused+Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Disabled+Pressed].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Editable+Enabled].backgroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Enabled].foregroundPainter with redPainter
Replacing the painter for ComboBox[Disabled+Editable].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[MouseOver].foregroundPainter with redPainter
Replacing the painter for ComboBox[Disabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.textField"[Disabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Enabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Editable+MouseOver].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.textField"[Selected].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Focused].backgroundPainter with NamedPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Editable+Pressed].backgroundPainter with redPainter
Replacing the painter for ComboBox:"ComboBox.arrowButton"[Pressed].foregroundPainter with redPainter
Replacing the painter for ComboBox[Editable+Enabled].backgroundPainter with NamedPainter
Replacing the painter for ComboBox[Focused+MouseOver].backgroundPainter with NamedPainter

当然,它看起来像什么: 在此处输入图片说明

但是,这并非我所期望的! 我以为用自定义redPainter替换了所有ComboBox: arrowButton Painter,我不会看到小的三角形黑色(或白色)箭头,而只会看到一个红色矩形。 另外,我没有管理者更改前景文本的颜色。 如何在组合菜单和选择弹出菜单中做到这一点?

[编辑]

进一步的研究:我尝试使用UIManager.getLookAndFeelDefaults().put而不是boxDefaults.put来放置属性,并且我得到了arrowButton的预期结果,该箭头显示为红色正方形(显然,对于经典和带花纹的组合)。 因此,我想我做错了什么,是覆盖拉皮条组合的属性,即我从Jasper Pott的博客中获得的两行内容

pimpedCombo.putClientProperty("Nimbus.Overrides", boxDefaults);
pimpedCombo.putClientProperty("Nimbus.Overrides.InheritDefaults", false);

有人可以帮忙吗?

[编辑2]

我还注意到如果我使用UIManager.put而不是UIManager.getLookAndFeelDefaults().put会出现不一致的行为,其中arrowButton仅在mouseOver或cliked等上显示为红色。Javadoc说UIManger.put仅影响“开发者默认值” ,不是L&F默认值。 有什么不同? 任何帮助,链接到有关一切工作原理的良好文档将很有帮助。

内部组件(例如JComboBox内的JTextField)的Nimbus.Overrides不起作用。 我已经向UIManager.getLookAndFeelDefaults()添加了必要的绘画工具,并实现了按实例分离父控件的自定义逻辑。

暂无
暂无

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

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