花了点时间看看了synth(swing定制外观),在网上找了半天没有看到一个非常完整的例子(包括代码,配置文件),所以自己写了一个完整的,放在这里.(当然参考了网上的).
1,java代码
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.synth.SynthLookAndFeel;
public class GUI {
public static void main(String[] args) throws Exception {
SynthLookAndFeel synth = new SynthLookAndFeel();
synth.load(GUI.class.getResourceAsStream("demo.xml"),
GUI.class);
UIManager.setLookAndFeel(synth);
JFrame jf = new JFrame();
jf.setSize(100, 100);
JButton b = new JButton();
jf.getContentPane().add(b);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
2,synth的配置文件,名字随便取,我这里用demo.xml
<synth>
<style id="button">
<state>
<imagePainter method="buttonBackground" path="images/dbroot.jpg"
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/db_open.jpg"
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/db_close.jpg"
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>
此文件放在src下面,里面涉及了三个图片,自己随便找三个吧.images文件夹建立在项目根目录下.(有很多人在这些问题上要纠结很久,我这里说得很清楚,按我的步骤做,绝对成功)
另,这里有比较详细的synth资料:http://www.ibm.com/developerworks/cn/java/j-synth/