【问题标题】:Change the look and feel of swing using Netbeans使用 Netbeans 更改摇摆的外观和感觉
【发布时间】:2020-04-27 12:17:16
【问题描述】:

我想改变 jframe 的外观。我使用网豆。于是我去找了jframe的源码,改了main方法在windows的look and feel中打开jframe。 下面是代码。它是由 netbeans 生成的,我改变了这一行 if ("Nimbus".equals(info.getName())) 改完后是这样的if ("Windows".equals(info.getName()))

这是由netbeans生成的main方法的完整代码。

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(TextEditorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TextEditorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TextEditorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TextEditorGui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new TextEditorGui().setVisible(true);
        }
    });
}

将 Nimbus 更改为 Windows 后,我运行文件 按 shift + f6,它使 jframe 具有 Windows 外观。但是当我按 F6 运行文件时,它给了我 Metal 的外观。是的,正是金属外观。不是 nimbus 外观.. 我想将其更改为 windows 外观。怎么做??请帮帮我...

【问题讨论】:

  • 如果您更改了它要查找的名称,但它找不到确切的名称,那么它不会调用 setLookAndFeel 并回退到默认值(我认为 仍然是 金属,即使过了这么多年)。打印出它循环的名称以找出可用的名称。
  • 实际上我打印了名称并得到了这个列表 Metal Nimbus CDE/Motif Windows Windows Classic

标签: java swing user-interface


【解决方案1】:

如果你使用最简单的调试方法并在for循环内添加System.out.println:

    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Windows".equals(info.getName())) {
            javax.swing.UIManager.setLookAndFeel(info.getClassName());
            break;
        }
        System.out.println(info);
    }

您将看到没有窗口的外观和感觉。这意味着UIManager.getInstalledLookAndFeels() 不会返回任何 Windows 外观。

方法:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

【讨论】:

  • for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { System.out.println(info.getName());实际上我使用了那个代码并且我收到了这个列表。 Metal Nimbus CDE/Motif Windows Windows Classic 但问题仍然存在。
  • @mastrmind-dev 你试过UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());吗?
  • 我明白了。关键是我必须更改主 java 文件。感谢您的建议
猜你喜欢
  • 2011-09-07
  • 2010-09-23
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
  • 2012-08-30
  • 1970-01-01
  • 2019-03-03
相关资源
最近更新 更多