This program calls the uimanager. getinstalledlookandfeels () method to obtain all the views installed on the local machine, and then creates a corresponding number of buttons to dynamically change the perception.
Related APIs:
static void
setLookAndFeel(LookAndFeel newLookAndFeel)
Set perception
static void
updateComponentTreeUI(Component c)
Dynamic Update View
static UIManager.LookAndFeelInfo[]
getInstalledLookAndFeels()
Obtain all views installed on the local machine
Execution result:
Code:
Package CN. youthol; import Java. AWT. *; import Java. AWT. event. *; import javax. swing. *; public class main {/*** author Bruce */public static void main (string [] ARGs) {eventqueue. invokelater (New runnable () {public void run () {myframe frame = new myframe ("Change lookandfeel"); frame. setdefaclocloseoperation (myframe. exit_on_close); frame. setvisible (true) ;}}) ;}/ ** Framework Window */class myframe extends jframe {private jpanel btnpanel;/** constructor */Public myframe (String title) {// set the title super (title); // set the size of setsize (800,600); // create the button panel btnpanel = new jpanel (); add (btnpanel ); // create buttonlookandfeel [] names = getalllookandfeels (); For (lookandfeel name: names) {createbutton (name. name, name. classname) ;}}/** get all views * lookandfeel is a custom class used to store view information */private lookandfeel [] getalllookandfeels () {uimanager. lookandfeelinfo [] Infos = uimanager. getinstalledlookandfeels (); lookandfeel [] lafs = new lookandfeel [Infos. length]; for (INT I = 0; I <lafs. length; ++ I) {lafs [I] = new lookandfeel () ;}for (INT I = 0; I <lafs. length; ++ I) {system. out. println (I); lafs [I]. classname = Infos [I]. getclassname (); lafs [I]. name = Infos [I]. getname () ;}return lafs ;}/ ** create button */private void createbutton (string name, final string uiname) {jbutton BTN = new jbutton (name); btnpanel. add (BTN); // sets the listener BTN. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {// change the perception try {uimanager. setlookandfeel (uiname); swingutilities. updatecomponenttreeui (myframe. this);} catch (exception ex) {ex. printstacktrace () ;}});}/** internal class lookandfeel * used to store view information */private class lookandfeel {string classname; // class name string name; // view name }}