3

我在一个项目中使用 Nimbus Look and Feel。然而,尽管每个 GUI JComponent 都有 Nimbus 的外观,但 JFrame 始终具有 Windows 外观。

JFrame 如何拥有 Nimbus 外观和感觉?

编辑:操作系统:Windows XP

4

5 回答 5

8

尝试使用这个:

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

于 2011-09-30T15:24:23.993 回答
4

确认@Andrew 的怀疑,setDefaultLookAndFeelDecorated()说,当得到支持时,“新创建JFrame的 s 将由 current 提供他们的Window装饰LookAndFeel。” 我更改了大小以查看整个标题。

框架外观

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");
    }
}
于 2011-09-30T22:28:24.730 回答
2

并根据 XP 的 Windows 经典 UI 进行确认。 在此处输入图像描述

于 2012-02-02T21:15:51.393 回答
1

你不能直接这样做,因为 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,不确定最佳方式。

于 2013-01-04T11:39:19.697 回答
0

我就是这样做的。我的 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();
                }
            }
        });
    }
于 2016-07-21T12:55:04.733 回答