黑马程序员技术交流社区
标题:
发一些java常用代码-java自定义按钮外观
[打印本页]
作者:
沈尤东
时间:
2011-7-24 11:51
标题:
发一些java常用代码-java自定义按钮外观
JAVA部份[code=java]import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.plaf.synth.SynthLookAndFeel;
public class MyButton {
JFrame frame = new JFrame("Test Buttons");
JButton jButton = new JButton("JButton"); // 按钮
public MyButton() {
frame.setLayout(new FlowLayout());
frame.getContentPane().add(jButton);
}
public void show() {
frame.pack();
frame.show();
}
public static void main(String[] args) {
MyButton tb = new MyButton();
tb.show();
SynthLookAndFeel slf = new SynthLookAndFeel();
try {
slf.load(MyButton.class.getResourceAsStream("mybutton.xml"), MyButton.class);
UIManager.setLookAndFeel(slf);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}[/code]XML部份[code=xml]<synth>
<style id="mybutton">
<state>
<imagePainter method="buttonBackground" path="mybutton.png" sourceInsets="3 6 12 20" paintCenter="true" stretch="true"/>
<insets top="3" left="6" bottom="12" right="20"/>
<font name="Aharoni" size="16"/>
</state>
<property key="Button.margin" type="insets" value="0 0 5 8"/>
</style>
<bind style="mybutton" type="region" key="Button"/>
</synth>[/code]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2