Java用synth自訂皮膚

來源:互聯網
上載者:User

1.  建立一個工程,工程名為untitled1

2.  建立一個應用程式(File->New->Application)

3.  在Frame1中添加一個按鈕

4.  在classes/untitled1目錄下添加圖象檔案(1_Down.gif、1_Off.gif、1_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.  其它控制項的使用方法類似。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.