如何更改JComboBox中的箭头样式

时间:2023-01-27 23:07:02

Let's say I want to use a custom image for the arrow in JComboBox, how can I do this?

假设我想在JComboBox中使用自定义图像作为箭头,我该怎么做?

I understand it's possible using the synth xml files, or maybe even UIManager.put(...), but I don't know how. All I want to do at this time is change the arrow image to something else, either programatically or even just overriding the image it uses. How exactly can I do this?

我知道可以使用synth xml文件,甚至可能使用UIManager.put(...),但我不知道如何使用。我此时想要做的就是将箭头图像更改为其他内容,无论是以编程方式还是仅覆盖其使用的图像。我怎么能这样做?

1 个解决方案

#1


20  

You can override createArrowButton() in BasicComboBoxUI. BasicArrowButton is a convenient starting point.

您可以在BasicComboBoxUI中覆盖createArrowButton()。 BasicArrowButton是一个方便的起点。

class ColorArrowUI extends BasicComboBoxUI {

    public static ComboBoxUI createUI(JComponent c) {
        return new ColorArrowUI();
    }

    @Override protected JButton createArrowButton() {
        return new BasicArrowButton(
            BasicArrowButton.SOUTH,
            Color.cyan, Color.magenta,
            Color.yellow, Color.blue);
    }
}

Then install it.

然后安装它。

JComboBox combo = new JComboBox();
combo.setUI(ColorArrowUI.createUI(combo));

#1


20  

You can override createArrowButton() in BasicComboBoxUI. BasicArrowButton is a convenient starting point.

您可以在BasicComboBoxUI中覆盖createArrowButton()。 BasicArrowButton是一个方便的起点。

class ColorArrowUI extends BasicComboBoxUI {

    public static ComboBoxUI createUI(JComponent c) {
        return new ColorArrowUI();
    }

    @Override protected JButton createArrowButton() {
        return new BasicArrowButton(
            BasicArrowButton.SOUTH,
            Color.cyan, Color.magenta,
            Color.yellow, Color.blue);
    }
}

Then install it.

然后安装它。

JComboBox combo = new JComboBox();
combo.setUI(ColorArrowUI.createUI(combo));