本文介绍了如果按钮样式已经存在,如何实现自定义Java合成按钮样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我尝试使用Java合成器创建自定义LookAndFeel,但在绑定自定义按钮时遇到问题. (退出按钮的外观有所不同).

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

这是创建按钮的代码.

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

我尝试删除普通的按钮样式,因此我所需要的只是自定义按钮,但这是行不通的.我还尝试使buttonStyle不包含任何内容,但这没有用,它只是获取了整体样式:

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

推荐答案

我相信您的问题是由于没有名为退出"的区域这一事实造成的.所有区域都应来自javax.swing.plaf.synth.Region类.该API将告诉您用于绑定到该区域的内容 http://docs.oracle.com/javase/6/docs/api/javax/swing/plaf/synth/Region.html

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

但是,如果您想要一个看起来与标准合成绘制按钮不同的特殊按钮,我发现最简单的方法是绑定到名称"而不是区域".创建一个扩展JButton的简单类.您可以将其命名为ExitButton.您甚至不需要重写任何方法.然后,XML文件将样式绑定到该类名称.然后,每当您要使用该样式按钮时,请创建一个ExitButton对象而不是JButton(尽管它的作用相同,并且具有相同的方法,但每个XML绑定看起来都不同).

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).

对于XML文件,您将如下进行绑定:

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

请注意,唯一的区别是type ="name和key =" ExitButton(或您选择命名扩展JButton的类的任何名称).此外,键的值必须与您创建并想要的类的名称匹配用于这种样式的按钮.

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.

希望这会有所帮助.

这篇关于如果按钮样式已经存在,如何实现自定义Java合成按钮样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:29