java - 在Swing中设置JComboBox的背景

标签 java swing background jcombobox

<分区>

我想要一个没有箭头按钮的 JComboBox(已完成),它在启用时具有绿色背景,在禁用时具有灰色背景(未完成)。我还为下拉列表使用自定义渲染器(完成)

我检查了 BasicComboBoxUI 的源代码并尝试重写一些方法,但没有任何反应。下拉列表始终具有灰色/蓝色背景。

这是我最后一次尝试的 SSCCE。我尝试了我能想到的一切。请给我一个提示,我迷路了。

    import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.plaf.basic.BasicComboBoxUI;

public class DropDownBackground
{
    public static void main(final String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                final JComboBox dropdown = new JComboBox(new DefaultComboBoxModel(new String[] { "one", "two", "three" }));
                dropdown.setRenderer(new ComboBoxListCellRenderer());
                dropdown.setUI(new BasicComboBoxUI()
                {
                    @Override
                    public void paint(final Graphics g, final JComponent c)
                    {

                        final Rectangle r = this.rectangleForCurrentValue();
                        this.paintCurrentValueBackground(g, r, true);
                        this.paintCurrentValue(g, r, true);

                    }

                    @Override
                    public void paintCurrentValueBackground(final Graphics g, final Rectangle bounds, final boolean hasFocus)
                    {
                        final Color t = g.getColor();
                        if (this.comboBox.isEnabled())
                            g.setColor(Color.GREEN);
                        else
                            g.setColor(Color.GRAY);
                        g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
                        g.setColor(t);
                    }

                    @Override
                    protected JButton createArrowButton()
                    {
                        return new JButton()
                        {
                            @Override
                            public int getWidth()
                            {
                                return 0;
                            }
                        };
                    }
                });
                dropdown.setBackground(Color.GREEN);
                final JPanel p = new JPanel();
                p.add(dropdown);

                final JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.getContentPane().add(new JScrollPane(p));
                f.setSize(800, 200);
                f.setLocation(0, 0);

                f.setVisible(true);

            }
        });

    }

    public static class ComboBoxListCellRenderer extends DefaultListCellRenderer
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus)
        {
            this.setToolTipText((String) value);
            if (isSelected)
            {
                this.setBackground(Color.RED);
                this.setForeground(Color.WHITE);
            }
            else
            {
                this.setBackground(Color.WHITE);
                this.setForeground(Color.BLACK);
            }

            this.setText((String) value);
            this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            return this;
        }
    }

}

最佳答案

因为我想广泛应用这种颜色,所以这是最好的方法:

UIManager.put("ComboBox.background", new ColorUIResource(UIManager.getColor("TextField.background")));
UIManager.put("ComboBox.foreground", new ColorUIResource(UIManager.getColor("TextField.foreground")));
UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.GREEN));
UIManager.put("ComboBox.selectionForeground", new ColorUIResource(Color.WHITE));

如果您想自定义更多(禁用颜色等),这个 UIManager 属性列表可能会有用:http://www.rgagnon.com/javadetails/JavaUIDefaults.txt

关于java - 在Swing中设置JComboBox的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14342495/

相关文章:

iphone - iOS:在特定时间在后台播放/停止音乐

java 2D 绘图

java - MigLayout 中的 JTextArea 导致 NullPointerException

java - JButton - 如何连接?

java - Apache HttpClient GET 与正文

iphone - 在后台模式下使用 App 接收通知

xcode - 从应用程序播放音频,即使它在后台

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

java - 基于添加重复值的HashSet vs TreeSet vs LinkedHashSet

java - 如何对旧的遗留应用程序进行单元测试