如果我有一个已经存在的按钮样式,我该如何实现自定义java合成按钮样式?

时间:2011-04-10 14:59:03

标签: java synth

所以我正在尝试使用java synth创建自定义LookAndFeel,并且我在绑定自定义按钮时遇到问题。 (退出按钮有不同的外观)。

以下是我的合成文件中的按钮:

<!-- Button -->

<style id="buttonStyle">
    <property key="Button.textShiftOffset" type="integer" value="1"/>
    <insets top="2" left="2" right="2" bottom="2"/>
    <state>
        <color value="#000000" type="BACKGROUND"/>
        <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/button.jpg" sourceInsets="2 2 2 2"/>   
    </state>
    <state value="PRESSED">
        <color value="#9BC3B1" type="BACKGROUND"/>
        <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/button_p.jpg" sourceInsets="2 2 2 2"/>         
    </state>
     <state value="MOUSE_OVER">
        <color value="#9BC3B1" type="BACKGROUND"/>
        <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/button_h.jpg" sourceInsets="2 2 2 2"/>         
    </state>
</style>
<bind style="buttonStyle" type="region" key="Button"/>


<!-- Exit Button -->

<style id="exitStyle">
    <property key="Button.textShiftOffset" type="integer" value="1"/>
    <insets top="1" left="1" right="1" bottom="1"/>
    <state>
        <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/exit.jpg" sourceInsets="2 2 2 2"/> 
    </state>
    <state value="PRESSED">
        <color value="#9BC3B1" type="BACKGROUND"/>
        <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/exit_p.jpg" sourceInsets="2 2 2 2"/>           
    </state>
    <state value="MOUSE_OVER">
        <color value="#9BC3B1" type="BACKGROUND"/>
        <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/exit_h.jpg" sourceInsets="2 2 2 2"/>           
    </state>
</style>
<bind style="exitStyle" type="region" key="Exit"/>

以下是创建按钮的代码。

JButton exit = new JButton("Exit");
        exit.setName("exit");

我已经尝试取出正常的按钮样式,所以我所拥有的只是自定义按钮,但是这不起作用。我也试过让buttonStyle没有任何内容,但是没有用,它只是选择了整体风格:

    <style id="backingStyle"> 
    <opaque value="TRUE"/>
    <font name="Dialog" size="11"/>
    <state>
      <color value="#2B271C" type="BACKGROUND"/>
      <color value="YELLOW" type="FOREGROUND"/>
    </state>
  </style>
  <bind style="backingStyle" type="region" key=".*"/>

2 个答案:

答案 0 :(得分:2)

我相信你的问题是因为没有名为Exit的区域。所有区域都应来自javax.swing.plaf.synth.Region类。 API将告诉您使用哪些内容绑定到该区域http://docs.oracle.com/javase/6/docs/api/javax/swing/plaf/synth/Region.html

但是如果你想要一个看起来与标准合成器绘制按钮不同的特殊按钮,我发现最简单的方法是绑定到&#34; name&#34;不是&#34;地区&#34;。创建一个扩展JButton的简单类。您可以将其命名为ExitButton。你甚至不需要覆盖任何方法。然后,XML文件将样式绑定到该类名。然后,只要你想使用那个样式按钮,就创建一个ExitButton对象而不是JButton(尽管它的行为相同,并且具有相同的方法,根据XML绑定,它看起来会有所不同。)

对于XML文件,您将按如下方式绑定它:

<!-- Exit Button -->

<style id="exitStyle">
  <property key="Button.textShiftOffset" type="integer" value="1"/>
  <insets top="1" left="1" right="1" bottom="1"/>
  <state>
    <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/exit.jpg" sourceInsets="2 2 2 2"/> 
  </state>
  <state value="PRESSED">
    <color value="#9BC3B1" type="BACKGROUND"/>
    <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/exit_p.jpg" sourceInsets="2 2 2 2"/>           
  </state>
  <state value="MOUSE_OVER">
    <color value="#9BC3B1" type="BACKGROUND"/>
    <imagePainter method="buttonBackground" path="/rstk/resources/pictures/parts/exit_h.jpg" sourceInsets="2 2 2 2"/>           
  </state>
</style>
<bind style="exitStyle" type="name" key="ExitButton"/>

请注意,唯一的区别是type =&#34; name和key =&#34; ExitButton&#34; (或者你选择用来命名扩展JButton的类)。此外,键的值必须与您创建的类的名称相匹配,并且要用于此样式的按钮。

希望这有帮助。

答案 1 :(得分:1)

当您绑定到命名组件时,您需要将绑定类型从“region”更改为“name”,并且键应该与您在组件“exit”上设置的名称相匹配(没有大写字母E)。所以你的退出按钮绑定行

<bind style="exitStyle" type="region" key="Exit"/>

应该是

<bind style="exitStyle" type="name" key="exit"/>

那应该是它!如果你遇到更多问题,请告诉我。