JFrame和Nimbus外观

3

我在一个项目中使用了Nimbus外观。然而,尽管每个GUI JComponent都有Nimbus的外观,但JFrame总是具有Windows的外观。

如何使JFrame具有Nimbus的外观?

编辑:操作系统:Windows XP


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");
    }
}

Frame title bar Look'n'Feel


3
起初我留了一条评论声称问题提出者肯定做错了什么,但我在你的回答中编辑的来源表明情况并非如此。在这里,虽然跨平台框架标题栏与Windows标题栏不同,但Nimbus标题栏是相同的。我怀疑这意味着Nimbus没有对标题栏进行特殊更改。其他操作系统的用户能否确认我的结果? - Andrew Thompson
1
非常错误:Nimbus不支持窗口装饰,您将始终获得系统窗口。 - Yago Méndez Vidal

4
确认 @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");
    }
}

1
@Andrew Thompson:这也似乎取决于平台当前的窗口管理器。 - trashgod

2

基于Windows XP经典界面进行确认。 enter image description here


请注意,Nimbus中窗格的背景颜色与Windows中窗格的背景颜色不同,并且是默认颜色。 - Spencer Kormos

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,最佳方法尚不确定。

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();
                }
            }
        });
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接