How can I implement a custom java synth button style if I have a button style that already exists?

随声附和 提交于 2019-12-11 11:01:55

问题


So I am trying to create a custom LookAndFeel using java synth and I am having problems binding a custom button. (the Exit Button has a different look).

Here are the buttons from my synth file:

<!-- 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"/>

Here is the code that creates the button.

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

I've tried taking out the normal button style, so that all I would have would be custom buttons, however that doesn't work. I also tried making the buttonStyle have nothing in it, but that didn't work, it just picked up the overall style:

    <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=".*"/>

回答1:


I believe your problem is due to the fact that there is no Region called Exit. All regions should come from the javax.swing.plaf.synth.Region class. The API will tell you what to use for binding to that region http://docs.oracle.com/javase/6/docs/api/javax/swing/plaf/synth/Region.html

But if you want to have a special button that looks different than your standard synth drawn button I find the easiest way is to bind to "name" not "region". Create a simple class that extends JButton. You can name it ExitButton. You don't even need to override any methods. The XML file will then bind a style to that class name. Then whenever you want to use that style button create an ExitButton object instead of a JButton (Though it will act the same and have the same methods it will look different per the XML binding).

For the XML file you will then bind it as follows:

<!-- 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"/>

Note that the only difference is type="name and key="ExitButton" (or whatever you choose to name your class that extends JButton). Also the value of the key must match the name of the class you created and want to use for this style of button.

Hope this helps.




回答2:


When you are binding to a named component you need to change the bind type from "region" to "name" and the key should match the name you set on the component "exit" (no capital E). So your line for the exit button binding

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

should be

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

That should be it! Tell me if you run into any more problems.



来源:https://stackoverflow.com/questions/5612587/how-can-i-implement-a-custom-java-synth-button-style-if-i-have-a-button-style-th

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!