如何在 main 中设置 Nimbus 外观

问题描述 投票:0回答:4

我刚刚学习Java,还没能解决我的这个小问题。

我的弹出日历使用 Nimbus 外观和感觉,但我有使用 Java 外观和感觉的面板和容器 Jtable - 我试图使每个 GUI 屏幕/窗口都使用 Nimbus 外观和感觉。 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.
    }

}
java swing nimbus
4个回答
13
投票

这就是我在主要方法中所做的,以启用 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();
}

在启动 swing 事件调度线程之前(调用 view.setVisible(true) 之前),您需要确保使用 Nimbus 外观配置 UIManager。


1
投票

我想尝试:

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

1
投票

要设置 Nimbus 的外观,请在您的 main 方法中添加以下代码:

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

0
投票

Saudações!

Criei uma Classe Nimbus.java.

没有执行文件 Main.java chamo imediatamente Nimbus.nimbus(); UIManager 的预定义设置为 Jframes 和 visíveis。

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class Nimbus {

    public static void nimbus(){

        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
            
    }
    
}

// No arquivo Main.java:

public class Main {

    public static void main(String[] args) {

        Nimbus.nimbus();
        
        Tela t = new Tela();
        
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.