简体   繁体   English

如何在 main 中设置 Nimbus 的外观和感觉

[英]How to set Nimbus look and feel in main

I am just learning Java and still have not been able to sort out this little problem I have.我只是在学习 Java,仍然无法解决我遇到的这个小问题。

My pop up Calendar uses Nimbus look and feel, but I have panels and container Jtables that use Java's look and feel - I am trying to make every GUI screen/ window use the Nimbus look and feel.我的弹出日历使用 Nimbus 外观和感觉,但我有使用 Java 外观和感觉的面板和容器 Jtables - 我试图让每个 GUI 屏幕/窗口都使用 Nimbus 外观和感觉。 It was suggested by Merky to put the following code in my main to make every subsequent screen have the Nimbus look and feel, but I cannot get it to work, so where and how should I put this code? Merky 建议将以下代码放在我的 main 中,以使每个后续屏幕都具有 Nimbus 的外观和感觉,但我无法让它工作,那么我应该在哪里以及如何放置此代码?

public static void main(String args[]) {
    SA md = new OptraderSA("Copyright© 2010 Simon Andi");

    Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();

    md.setLocation(sd.width/2-400/2, sd.height/2-400/2);
    md.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    /*Suggested Code*/
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                System.out.println("CHOSEN THIS");
                break;
            } else {
                UIManager.setLookAndFeel  ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, you can set to another look and feel.
        // I can't get it to compile or work.
    }

}

This is what I do in my main method to enable Nimbus look and feel:这就是我在启用 Nimbus 外观和感觉的主要方法中所做的:

public static void main(String[] args) {
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {
        // If Nimbus is not available, fall back to cross-platform
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception ex) {
            // Not worth my time
        }
    }
    new Controller();
}

You need to be sure to configure the UIManager with the Nimbus look and feel before you start the swing event dispatch thread (before calling view.setVisible(true)).在启动 Swing 事件调度线程之前(在调用 view.setVisible(true) 之前),您需要确保使用 Nimbus 外观和感觉配置 UIManager。

I think try with:我认为尝试:

for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        System.out.println("CHOSEN THIS");
        break;
    }
}

To set Nimbus look and feel, add this code in your main method:要设置 Nimbus 的外观和感觉,请在您的 main 方法中添加以下代码:

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM