Gui program based on AWT and swing-change view

Source: Internet
Author: User

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 voidsetLookAndFeel(LookAndFeel newLookAndFeel)

Set perception

static voidupdateComponentTreeUI(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 }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.