繁体   English   中英

设置Nimbus外观和感觉Java教程 - 永远不会到catch块来设置另一种外观

[英]Set Nimbus Look and Feel Java Tutorials - will never get to the catch block to set another look and feel

Java教程提供了以下用于设置Nimbus外观的示例: Java教程 - Nimbus外观和感觉

import javax.swing.UIManager.*;

try {
    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
            UIManager.setLookAndFeel(info.getClassName());
            break;
        }
    }
} catch (Exception e) {
    // If Nimbus is not available, you can set the GUI to another look and feel.
}

在我看来,仅当UIManager.setLookAndFeel()抛出异常时才会发生捕获。 如果你把代码放在catch块中设置另一个外观,理论上它应该永远不会执行,因为你只在已安装的LookAndFeel上使用.setLookAndFeel() 如果Nimbus在系统上不可用,它将通过ForEach进入try-catch块,而不是设置任何东西。

不应该使用“如果nimbus不可用......”的代码去ForEach和catch之前吗? 在catch块中,您可能希望指定随JAVA提供的SystemLookAndFeel或DefaultLookAndFeel。

我是否正确如上所述的try-catch块? 我担心我错过了一些东西,因为这个示例代码已被多次引用,我已经看到了这个问题。

Nimbus始终可以从类级别获得,因为它包含在Java 1.5之后的每个JRE中。

如果系统无法通过反射机制中的任何问题加载对象,那么“不可用”的含义是可能的。

在任何情况下,我都会说你应该在finally块中设置另一个Look-and-Feel。 这样,即使在较低版本的Java中,您也可以加载备选方案。

暂无
暂无

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

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