Switch between the local appearance of swing and the appearance of substance and the solution

Source: Internet
Author: User

Swing applications can be easy to switch between open source Look&&feel, but the problem comes when you switch the application to the default or JDK-like appearance under open source. Not without the title bar, that is, the appearance of the title bar has not changed, with the system window decoration. These are caused by adding such a code to the main method when the application starts:

Jframe.setdefaultlookandfeeldecorated (TRUE);

The solution to this problem is to dispose of the original JFrame first, then a JFrame in new, so that the original frame to point to the new JFrame. Not much to say, look at the code more intuitive, the core code is as follows:

Initialization application, the Initcomponents () method is generated by the NetBeans IDE and is not posted.

private static JFrame config;
Private Rectangle savedbounds;



/** creates new form Config */public
Skinchangedemo () {
initcomponents ();
Public

Skinchangedemo (Boolean decor) {
setundecorated (decor);
Initcomponents ();
}

Here's the core code:

 private void Saharabuttonactionperformed (Java.awt.event.ActionEvent evt) {
        TODO Add your handling code here:lookandfeel old = Uimanager.getlookandfeel ();
        Substanceskin skin = new Saharaskin ();
        if (old instanceof Substancelookandfeel) {Substancelookandfeel.setskin (skin);
        else {//If the appearance is not substance, switch to substance appearance changskin (skin); }} private void Nimbusbuttonactionperformed (Java.awt.event.ActionEven T evt) {try {uimanager.setlookandfeel (new Nimbuslookand
            Feel ());
            Savedbounds = GetBounds ();
            Dispose ();
            config = null;
            Config = new Skinchangedemo (false);
            Config.setbounds (Savedbounds);
        Config.setvisible (TRUE); catch (Unsupportedlookandfeelexception ex) {LoGger.getlogger (SkinChangeDemo.class.getName ()). log (Level.severe, NULL, ex);

    } swingutilities.updatecomponenttreeui (this); } private void businessbuttonactionperformed (Java.awt.event.ActionEvent ev
        T) {Lookandfeel old = Uimanager.getlookandfeel ();
        Substanceskin skin = new Businessskin ();
        if (old instanceof Substancelookandfeel) {Substancelookandfeel.setskin (skin);
        else {//If the appearance is not substance, switch to substance appearance changskin (skin);
     }/** * is used to make the interface of a substance appearance the substance appearance.
        * @param skin */private void Changskin (Substanceskin skin) {savedbounds = GetBounds ();
        Dispose ();
        Config = new Skinchangedemo (true);  Config.getrootpane (). Setwindowdecorationstyle (Jrootpane.frame); This is the key code, see your own API Experience bar Config.setbounDS (Savedbounds);
        Maintain the change of the skin when the position is Substancelookandfeel.setskin (skin);
        Config.setvisible (TRUE);
        Config.getrootpane (). UpdateUI ();

    Swingutilities.updatecomponenttreeui (this); }

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.