简体   繁体   English

如何使 Swing 应用程序具有黑暗灵气主题 netbeans

[英]How to make a swing application have dark nimbus theme netbeans

So I am new to Netbeans and Swing GUI development in general and I am trying to change the Look and Feel of a JFrame.所以我一般是 Netbeans 和 Swing GUI 开发的新手,我正在尝试更改 JFrame 的外观和感觉。 When I create a JFrame form Netbeans by default make it Nimbus theme.当我创建一个 JFrame 表单时,Netbeans 默认将其设为 Nimbus 主题。

I tried to change to the Windows theme ( if ("Windows".equals(info.getName())) { ) and the Metal theme ( if ("Metal".equals(info.getName())) { ) and it worked flawlessly with these 2 themes.我尝试更改为 Windows 主题( if ("Windows".equals(info.getName())) { )和 Metal 主题( if ("Metal".equals(info.getName())) { )和它与这两个主题完美配合。

But when I try to change it to the Dark Nimbus theme ( if ("Dark Nimbus".equals(info.getName())) { ) it didn't work.但是当我尝试将其更改为 Dark Nimbus 主题时( if ("Dark Nimbus".equals(info.getName())) { )它没有用。

I also tried doing Right Click > Preview Design > Dark Nimbus and yes it previews the Dark Nimbus theme as expected.我还尝试过右键单击 > 预览设计 > Dark Nimbus,是的,它按预期预览了 Dark Nimbus 主题。 But not when I actually compile and run the program (by clicking the play button).但是当我实际编译和运行程序时(通过单击播放按钮)。

Does anyone know how to change the theme to "Dark Nimbus"?有谁知道如何将主题更改为“Dark Nimbus”?

Here is the relevant code:这是相关的代码:

    /* 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 ("Dark Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(TestGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>
    //</editor-fold>

I recently had the same concern and I found these excelent tips from Geertjan Wielenga and from Neil C Smith .我最近也有同样的担忧,我从Geertjan WielengaNeil C Smith那里找到了这些出色的技巧。 So the answer to your question would be:所以你的问题的答案是:

public static void main(String[] args) {
  UIManager.put( "control", new Color( 128, 128, 128) );
  UIManager.put( "info", new Color(128,128,128) );
  UIManager.put( "nimbusBase", new Color( 18, 30, 49) );
  UIManager.put( "nimbusAlertYellow", new Color( 248, 187, 0) );
  UIManager.put( "nimbusDisabledText", new Color( 128, 128, 128) );
  UIManager.put( "nimbusFocus", new Color(115,164,209) );
  UIManager.put( "nimbusGreen", new Color(176,179,50) );
  UIManager.put( "nimbusInfoBlue", new Color( 66, 139, 221) );
  UIManager.put( "nimbusLightBackground", new Color( 18, 30, 49) );
  UIManager.put( "nimbusOrange", new Color(191,98,4) );
  UIManager.put( "nimbusRed", new Color(169,46,34) );
  UIManager.put( "nimbusSelectedText", new Color( 255, 255, 255) );
  UIManager.put( "nimbusSelectionBackground", new Color( 104, 93, 156) );
  UIManager.put( "text", new Color( 230, 230, 230) );
  try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
      if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
      }
    }
  } catch (ClassNotFoundException e) {
    e.printStackTrace();
  } catch (InstantiationException e) {
    e.printStackTrace();
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (javax.swing.UnsupportedLookAndFeelException e) {
    e.printStackTrace();
  } catch (Exception e) {
    e.printStackTrace();
  }
  // Show your JFrame
}

The code runs better in this state:代码在这种状态下运行得更好:

// Dark LAF
try {
    UIManager.setLookAndFeel(new NimbusLookAndFeel());
    UIManager.put("control", new Color(128, 128, 128));
    UIManager.put("info", new Color(128, 128, 128));
    UIManager.put("nimbusBase", new Color(18, 30, 49));
    UIManager.put("nimbusAlertYellow", new Color(248, 187, 0));
    UIManager.put("nimbusDisabledText", new Color(128, 128, 128));
    UIManager.put("nimbusFocus", new Color(115, 164, 209));
    UIManager.put("nimbusGreen", new Color(176, 179, 50));
    UIManager.put("nimbusInfoBlue", new Color(66, 139, 221));
    UIManager.put("nimbusLightBackground", new Color(18, 30, 49));
    UIManager.put("nimbusOrange", new Color(191, 98, 4));
    UIManager.put("nimbusRed", new Color(169, 46, 34));
    UIManager.put("nimbusSelectedText", new Color(255, 255, 255));
    UIManager.put("nimbusSelectionBackground", new Color(104, 93, 156));
    UIManager.put("text", new Color(230, 230, 230));
    SwingUtilities.updateComponentTreeUI(this);
} catch (UnsupportedLookAndFeelException exc) {
    System.err.println("Nimbus: Unsupported Look and feel!");
}

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

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