合成器示例

在标题为一个 GroupLayout 示例的类中,使用GroupLayout创建了一个名为“查找”的搜索对话框。创建对话框Find.java的程序使用具有“海洋”主题的跨平台(“金属”)外观:

Find.

本课使用外部 XML 文件创建与 Synth 相同的对话框。这是SynthDialog.java文件的列表。

SynthDialog.javaFind.java完全相同,但initLookAndFeel()方法已更改为使用 Synth 外观和名为synthDemo.xml的外部文件。这是新的initLookAndFeel()方法:

private static void initLookAndFeel() {
   SynthLookAndFeel lookAndFeel = new SynthLookAndFeel();
       
   // SynthLookAndFeel load() method throws a checked exception
   // (java.text.ParseException) so it must be handled
   try {
      lookAndFeel.load(SynthDialog.class.getResourceAsStream("synthDemo.xml"),
                                                               SynthDialog.class);
      UIManager.setLookAndFeel(lookAndFeel);
   } 
            
   catch (ParseException e) {
      System.err.println("Couldn't get specified look and feel ("
                                   + lookAndFeel
                                    + "), for some reason.");
      System.err.println("Using the default look and feel.");
      e.printStackTrace();
   }
}

XML 文件

XML 文件synthDemo.xml以绑定到所有区域的样式开始。这样做是一种很好的做法,以确保没有绑定样式的区域将包含某些内容。这种样式使所有区域都以不透明的颜色绘制背景。它还设置默认字体和默认颜色。

<!-- Style that all regions will use -->
  <style id="backingStyle">
    <!-- Make all the regions opaque-->
    <opaque value="TRUE"/>
    <font name="Dialog" size="14"/>
    <state>
      <color value="#D8D987" type="BACKGROUND"/>
      <color value="RED" type="FOREGROUND"/>
    </state>
  </style>
  <bind style="backingStyle" type="region" key=".*"/>

Notes:

1.颜色定义必须在\ 元素内。这允许根据状态改变颜色。 backingStyle中的\ 元素没有属性,因此适用于所有区域,无论其状态如何。如果区域具有其他状态,则将这些状态与优先级合并,这些优先级将赋予文件中稍后出现的状态定义。

2.字体定义不在\ 元素内,因为状态发生变化时字体不应更改(许多组件的大小取决于其字体,并且字体的更改可能会导致组件的大小意外更改)。

定义的下一个\ 元素是用于使用图像绘制的文本字段。

<style id="textfield">
    <insets top="4" left="6" bottom="4" right="6"/>
    <state>
       <font name="Verdana" size="14"/>
       <color value="#D2DFF2" type="BACKGROUND"/>
       <color value="#000000" type="TEXT_FOREGROUND"/>
    </state>
    <imagePainter method="textFieldBorder" path="images/textfield.png"
                  sourceInsets="4 6 4 6" paintCenter="false"/>
  </style>
  <bind style="textfield" type="region" key="TextField"/>

Notes:

1.字体和颜色定义将覆盖backingStyle中的定义。

  1. insetssourceInsets被赋予相同的值,这只是一个巧合,因为它们彼此无关。

3.背景色#D2DFF2 是浅蓝色-与图像中的背景textfield.png相同的颜色。

  1. paintCenterfalse,以便您可以看到背景颜色。

下一个\ 元素用于绘制有不同图像的按钮,具体取决于按钮状态。当鼠标经过按钮时,其外观会发生变化。单击(按下)后,图像再次更改。

<style id="button">
        <!-- Shift the text one pixel when pressed -->
    <property key="Button.textShiftOffset" type="integer" value="1"/>
    <!-- set size of buttons -->
    <insets top="15" left="20" bottom="15" right="20"/>
    <state>
      <imagePainter method="buttonBackground" path="images/button.png"
                           sourceInsets="10 10 10 10" />
      <font name="Dialog" size="16"/>
      <color type="TEXT_FOREGROUND" value="#FFFFFF"/>
    </state>
              
    <state value="PRESSED"> 
      <imagePainter method="buttonBackground"
          path="images/button_press.png"
                   sourceInsets="10 10 10 10" />
    </state>
            
    <state value="MOUSE_OVER">    
      <imagePainter method="buttonBackground"
          path="images/button_over.png"
                 sourceInsets="10 10 10 10" />
    </state>
  </style>
  <bind style="button" type="region" key="Button"/>

Notes:

1.<state>元素中没有属性的字体和颜色定义适用于所有按钮状态。这是因为所有适用状态的定义(而没有属性的 元素就是其中之一)将合并,并且没有其他字体和颜色定义可以优先。

  1. sourceInsets值足够大,不会拉伸按钮图像的弯曲角。

  2. PRESSEDMOUSE_OVER状态的 Sequences 很重要。由于鼠标在按下时始终位于按钮上方,因此两种状态都将应用于按下的按钮,并且定义的第一个状态(PRESSED)将适用。当鼠标悬停在按钮上但未被按下时,仅MOUSE_OVER状态适用。如果PRESSEDMOUSE_OVER状态的 Sequences 颠倒了,则将永远不会使用PRESSED状态图像。

下一个\ 元素用于使用不同图标绘制的复选框,具体取决于复选框的状态。

<style id="checkbox">
    <imageIcon id="check_off" path="images/checkbox_off.png"/>
    <imageIcon id="check_on" path="images/checkbox_on.png"/>
    <property key="CheckBox.icon" value="check_off"/>
    <state value="SELECTED">   
      <property key="CheckBox.icon" value="check_on"/>
    </state>
  </style>
  <bind style="checkbox" type="region" key="Checkbox"/>

Notes:

1.您必须使用\ 元素定义要使用的任何图标。

2.<insets>元素和sourceInsets属性不与图标一起使用,因为它们以固定大小呈现且未拉伸。

3.用于呈现复选框的图标是在CheckBox.icon属性中命名的图标。 (请参见javax/swing/plaf/synth/doc-files/componentProperties.html),它是 id =“ check_off”的图标,除非复选框状态为SELECTED

synthDemo.xml文件由上述样式构成,并包装在\ </synth>标记中。您可以通过单击synthDemo.xml打开完成的文件。

Try this:

单击启动按钮以使用Java™Web 开始(下载 JDK 7 或更高版本)运行 SynthDialog 示例。另外,要自己编译和运行示例,请咨询示例索引\ </ a>。