我想使一个下拉列表,看起来像这样:我如何定制JComboBox的外观?
alt text http://i44.tinypic.com/v80z81.png
的JComboBox提供我所需要的功能,所以我不应该需要编写一个自定义组件。但是我找不到一种方法来自定义JComboBox的整个外观来实现这一点。有什么建议么?
更新:我不想让所有的JComboBox看起来像这样。我只想创建一个这个自定义的外观。所以据我所知,创建自定义L不是解决方案。
我想使一个下拉列表,看起来像这样:我如何定制JComboBox的外观?
alt text http://i44.tinypic.com/v80z81.png
的JComboBox提供我所需要的功能,所以我不应该需要编写一个自定义组件。但是我找不到一种方法来自定义JComboBox的整个外观来实现这一点。有什么建议么?
更新:我不想让所有的JComboBox看起来像这样。我只想创建一个这个自定义的外观。所以据我所知,创建自定义L不是解决方案。
的JComboBox UI从文本框,列表和箭头组成。 自定义它们,你可以使用一些现成大号& F或用合成器大号实现最容易& F.
初始化大号& 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组件的外观如何,您不能只是。
但如果你只需要简单修改颜色重用默认大号&男,你可以使用下面的代码片段来初始化组合框的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尝试更新你的样子和感觉您的组合框不回去了错误的颜色。
你有没有找到答案? – 2010-06-09 17:46:56
@Stephane,我最终创建了一个自定义组件。 – 2010-06-10 11:15:41