我想制作一个如下所示的下拉列表:
替代文字http://i44.tinypic.com/v80z81.png
JComboBox提供了我需要的功能,因此我不需要编写自定义组件.但我无法找到一种方法来自定义JComboBox的整个外观来实现这一目标.有什么建议?
更新:我不希望所有JComboBox都像这样.我只想用这个自定义外观创建一个.据我所知,创建自定义L&F不是解决方案.
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);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
使用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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6465 次 |
| 最近记录: |