如何自定义JComboBox的外观?

问题描述 投票:3回答:2

我想制作一个如下所示的下拉列表:

alt text

JComboBox提供了我需要的功能,因此我不需要编写自定义组件。但我无法找到一种方法来自定义JComboBox的整个外观来实现这一目标。有什么建议?

更新:我不希望所有JComboBox都像这样。我只想用这个自定义外观创建一个。据我所知,创建自定义L&F不是解决方案。

java swing
2个回答
8
投票

JComboBox UI由文本字段,列表和箭头组成。要定制它们,您可以使用一些现成的L&F或使用Synth L&F轻松实现。

初始化L&F:

SynthLookAndFeel lookAndFeel = new SynthLookAndFeel();

try {
    lookAndFeel.load(YourClassAlongWithSynthXml.class.getResourceAsStream("synth.xml"), YourClassAlongWithSynthXml.class);
} catch (ParseException e) {
    e.printStackTrace();
}

UIManager.setLookAndFeel(lookAndFeel);

synth.xml包含这样的内容:

<style id="textfield">
    <insets top="4" left="6" bottom="4" right="6" />

    <state>
        <font name="Verdana" size="12" />

        <!--
        <color type="BACKGROUND" value="#D2DFF2" />
        -->
        <color type="TEXT_FOREGROUND" value="#003c2d" />
    </state>

    <imagePainter method="textFieldBorder" path="images/textfield.png" sourceInsets="4 6 4 6" paintCenter="false" />
</style>

<bind style="textfield" type="region" key="TextField" />

<style id="arrowStyle">
    <imagePainter method="arrowButtonForeground" path="images/arrow-up.png" sourceInsets="0 0 0 0" stretch="false" direction="north" />
    <imagePainter method="arrowButtonForeground" path="images/arrow-down.png" sourceInsets="0 0 0 0" stretch="false" direction="south" />
    <imagePainter method="arrowButtonForeground" path="images/arrow-left.png" sourceInsets="0 0 0 0" stretch="false" direction="west" />
    <imagePainter method="arrowButtonForeground" path="images/arrow-right.png" sourceInsets="0 0 0 0" stretch="false" direction="east" />
</style>

<bind key="ArrowButton" type="region" style="arrowStyle" />

<style id="comboArrowStyle">
    <imagePainter method="arrowButtonForeground" path="images/combobox-arrow.png" sourceInsets="0 0 0 0" stretch="false" direction="south" />
</style>

使用Synth L&F,您需要完全定义UI组件的外观,而不仅仅是这样。

但是,如果您只需要简单地修改重新使用默认L&F的颜色,则可以使用以下代码段来初始化组合框的UI:

final Color COLOR_BUTTON_BACKGROUND = Color.decode("#d3dedb");

UIManager.put("ComboBox.buttonBackground", COLOR_BUTTON_BACKGROUND);
UIManager.put("ComboBox.buttonShadow", COLOR_BUTTON_BACKGROUND);
UIManager.put("ComboBox.buttonDarkShadow", COLOR_BUTTON_BACKGROUND);
UIManager.put("ComboBox.buttonHighlight", COLOR_BUTTON_BACKGROUND);

1
投票

如果你不想创建一个完整的外观,最简单的答案是子类jcombobox并覆盖paintComponent方法并绘制你想要的任何东西。

用你的灰色填充背景,然后用浅色绘制你的文字和箭头。

如果你走这条路线,你可能还需要覆盖updateUI方法,这样如果VM试图更新你的外观并感觉你的组合框没有回到错误的颜色。

© www.soinside.com 2019 - 2024. All rights reserved.