簡體   English   中英

JFrame和Nimbus外觀和感覺

[英]JFrame and Nimbus Look And Feel

我在項目中使用Nimbus Look and Feel。 但是,盡管每個GUI JComponent都具有Nimbus的外觀,但JFrame始終具有Windows外觀。

JFrame如何擁有Nimbus外觀和感覺?

編輯:操作系統:Windows XP

試試這個:

JFrame.setDefaultLookAndFeelDecorated(true); //before creating JFrames

有關詳細信息,請參閱教程中的如何設置外觀


import javax.swing.*;

class FrameLook {

    public static void showFrame(String plaf) {
        try {
            UIManager.setLookAndFeel(plaf);
        } catch(Exception e) {
            e.printStackTrace();
        }
        JFrame f = new JFrame(plaf);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.setSize(400,100);
        f.setLocationByPlatform(true);
        f.setDefaultLookAndFeelDecorated(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        showFrame(UIManager.getSystemLookAndFeelClassName());
        showFrame(UIManager.getCrossPlatformLookAndFeelClassName());
        showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    }
}

框架標題欄Look'n'Feel

確認@Andrew的懷疑, setDefaultLookAndFeelDecorated()表示,當支持時,“新創建的JFrame將由當前的LookAndFeel提供他們的Window裝飾。” 我改變了大小以查看整個標題。

FrameLook

import javax.swing.*;

class FrameLook {

    public static void showFrame(String plaf) {
        try {
            UIManager.setLookAndFeel(plaf);
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }
        JFrame f = new JFrame(plaf);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        f.setSize(500, 100);
        f.setLocationByPlatform(true);
        JFrame.setDefaultLookAndFeelDecorated(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        showFrame(UIManager.getSystemLookAndFeelClassName());
        showFrame(UIManager.getCrossPlatformLookAndFeelClassName());
        showFrame("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    }
}

並基於適用於XP的Windows經典UI進行確認。 在此輸入圖像描述

你不能直接這樣做,因為Nimbus不支持窗口裝飾,這就是為什么你總是得到一個系統窗口,即使有給定的答案。 試試這個非常簡單的代碼:

import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class DoesNimbusSupportWindowDecorations {

    @SuppressWarnings("unchecked")
    public static void main(String... args) {
        LookAndFeel nimbus = null;
        for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) {
            if (lafInfo.getName() == "Nimbus") {
                try {
                    nimbus = ((Class<LookAndFeel>) Class.forName(
                            lafInfo.getClassName())).newInstance();
                } catch (Exception e) {
                    System.err.println("Unexpected exception.");
                }
            }
        }

        if (nimbus != null) {
            System.out.println("Nimbus supports window decorations...? "
                    + (nimbus.getSupportsWindowDecorations() ? "YES" : "NO"));
        } else {
            System.err.println("Your system does not support Nimbus, you can't"
                    + " run this test.");
        }
    }

}

或者只是在代碼中使用正確的導入:

System.out.println(new NimbusLookAndFeel().getSupportsWindowDecorations());

超出我理解的是為什么Sun決定這樣的東西,因為裝飾確實存在於內部框架並且具有定制裝飾。 我將調查是否可以通過擴展NimbusLookAndFeel或使用默認值來使用這些裝飾,因為Nimbus基於Synth,不確定最佳方式。

這就是我的方式。 來自我的eclipse項目的復制粘貼。

import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.EventQueue;
import java.awt.BorderLayout;
import javax.swing.*;
public class Frame1 {
    private JFrame frame;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                     for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                            if ("Nimbus".equals(info.getName())) {
                                UIManager.setLookAndFeel(info.getClassName());
                                break;
                            }
                        }
                Frame1 window = new Frame1();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM