Java用synth自定义皮肤

    技术2022-05-11  75

    1.  新建一个工程,工程名为untitled1

    2.  新建一个应用程序(File->New->Application

    3.  Frame1中添加一个按钮

    4.  classes/untitled1目录下添加图象文件(1_Down.gif1_Off.gif1_On.gif,分别表示按钮的三种状态)和demo.xml文件

    Demo.xml中的内容是:

    <synth>

      <style id="button">

          <state>

        <imagePainter method="buttonBackground" path="images/1_On.gif"

                      sourceInsets="9 10 9 12" paintCenter="true" stretch="true"/>

        <insets top="9" left="10" bottom="9" right="12"/>

        <font name="Aharoni" size="16"/>

        <color type="TEXT_FOREGROUND" value="#FFFFFF"/>

          </state>

          <state value="MOUSE_OVER">

        <imagePainter method="buttonBackground" path="images/1_Down.gif"

                      sourceInsets="9 10 9 12" paintCenter="true" stretch="true"/>

        <insets top="9" left="10" bottom="9" right="12"/>

        <color type="TEXT_FOREGROUND" value="#FFFFFF"/>

          </state>

          <state value="PRESSED">

        <imagePainter method="buttonBackground" path="images/1_Off.gif"

                      sourceInsets="10 12 8 9" paintCenter="true" stretch="true"/>

        <insets top="10" left="12" bottom="8" right="9"/>

        <color type="TEXT_FOREGROUND" value="#FFFFFF"/>

          </state>

        <property key="Button.margin" type="insets" value="0 0 0 0"/>

      </style>

      <bind style="button" type="region" key="Button"/>

    </synth>

    5.  Application1.java文件的头部加入以下语句:

    import javax.swing.plaf.synth.SynthLookAndFeel;

    6.  public void run()函数做如下的修改:

    public void run() {

                try {

                 

                    //添加以下3个语句

              SynthLookAndFeel synth = new SynthLookAndFeel();

              synth.load(Application1.class.getResourceAsStream("demo.xml"),Application1.class);

              UIManager.setLookAndFeel(synth);

                   

              /*原先的语句          

              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

                    */

            }

    7.  编译运行,便可看到结果了。

        

    8.  其它控件的使用方法类似。

     

    最新回复(0)