1

因此,我正在尝试使用 java 合成器创建自定义 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=".*"/>
4

2 回答 2

2

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

但是,如果您想要一个看起来与标准合成器绘制按钮不同的特殊按钮,我发现最简单的方法是绑定到“名称”而不是“区域”。创建一个扩展 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="name 和 key="ExitButton"(或者您选择命名扩展 JButton 的类的任何名称)。此外,键的值必须与您创建并希望用于的类的名称匹配这种风格的按钮。

希望这可以帮助。

于 2012-06-11T21:42:24.547 回答
1

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

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

应该

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

应该是这样!如果您遇到更多问题,请告诉我。

于 2011-07-28T21:55:54.487 回答