入门基础:Java用synth自定义皮肤

王朝java/jsp·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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. 其它控件的使用方法类似。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航