5

我刚刚学习Java,仍然无法解决我遇到的这个小问题。

我的弹出日历使用 Nimbus 外观,但我有面板和容器 Jtables 使用 Java 的外观 - 我试图让每个 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.
    }

}
4

3 回答 3

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。

于 2011-01-06T17:55:28.900 回答
1

我想尝试:

for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
        UIManager.setLookAndFeel(info.getClassName());
        System.out.println("CHOSEN THIS");
        break;
    }
}
于 2011-03-23T12:08:35.697 回答
1

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

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
于 2018-04-03T06:47:02.717 回答