java观感示例分享
package com.hongyuan.gui;
import java.awt.eventqueue;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jpanel;
import javax.swing.swingutilities;
import javax.swing.uimanager;
import javax.swing.unsupportedlookandfeelexception;
public class plaftest {
public static void main(string[] args) {
eventqueue.invokelater(new runnable() {
@override
public void run() {
plafframe frame=new plafframe();
frame.setdefaultcloseoperation(jframe.exit_on_close);
frame.setvisible(true);
}
});
}
}
class plafframe extends jframe
{
private jpanel buttonpanel;
public plafframe(){
this.settitle("plaftest");
this.setsize(400, 300);
buttonpanel=new jpanel();
//查询观感并生成按钮
uimanager.lookandfeelinfo[] infos=uimanager.getinstalledlookandfeels();
for(uimanager.lookandfeelinfo info:infos){
makebutton(info.getname(),info.getclassname());
}
this.add(buttonpanel);
}
void makebutton(string name,final string plafname){
jbutton button=new jbutton(name);
buttonpanel.add(button);
button.addactionlistener(new actionlistener() {
@override
public void actionperformed(actionevent e) {
try {
//设置观感并更新组件
uimanager.setlookandfeel(plafname);
swingutilities.updatecomponenttreeui(plafframe.this);
} catch (classnotfoundexception | instantiationexception
| illegalaccessexception
| unsupportedlookandfeelexception e1) {
e1.printstacktrace();
}
}
});
}
}
上一篇: JSP开发导引