繁体   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