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);
如果您不想创建整个外观,最简单的方法是子类化jcombobox并覆盖paintComponent方法,并绘制您想要的任何内容。
用您的灰色填充背景,然后用较浅的颜色绘制您的文本和箭头。
如果您选择这种方法,您可能还需要覆盖updateUI方法,以便如果VM尝试更新您的外观,您的组合框不会回到错误的颜色。