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

所以我试图使用java合成器创建自定义LookAndFeel,并且我在绑定自定义按钮时遇到问题。 (退出按钮具有不同的外观)。 以下是我的synth文件中的按钮:
<!-- 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=".*"/>
    
已邀请:
我相信你的问题是因为没有名为Exit的区域。所有区域都应来自javax.swing.plaf.synth.Region类。 API将告诉您使用什么来绑定到该区域http://docs.oracle.com/javase/6/docs/api/javax/swing/plaf/synth/Region.html 但是如果你想要一个看起来与标准合成器绘制按钮不同的特殊按钮,我发现最简单的方法是绑定到“name”而不是“region”。创建一个扩展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 and key =”ExitButton“(或者您选择为您的类命名扩展JButton的任何内容)。此外,键的值必须与您创建并希望用于的类的名称相匹配这种风格的按钮。 希望这可以帮助。     
当您绑定到命名组件时,需要将绑定类型从“region”更改为“name”,并且键应与您在组件“exit”上设置的名称相匹配(无大写字母E)。所以你的退出按钮绑定行
<bind style="exitStyle" type="region" key="Exit"/>
应该
<bind style="exitStyle" type="name" key="exit"/>
那应该是它!告诉我你是否遇到了更多问题。     

要回复问题请先登录注册