所以我不熟悉Netbeans和Swing GUI开发,我试图改变JFrame的外观。当我创建一个JFrame窗体时,Netbeans默认使它成为Nimbus主题。
我尝试更改为Windows主题(if ("Windows".equals(info.getName())) {
)和Metal主题(if ("Metal".equals(info.getName())) {
),并且它与这两个主题完美配合。
但是当我尝试将其更改为Dark Nimbus主题(if ("Dark Nimbus".equals(info.getName())) {
)时,它无法正常工作。
我也试过做右键点击>预览设计> Dark Nimbus,是的,它按预期预览了Dark Nimbus主题。但是当我实际编译并运行程序时(通过单击播放按钮)不是。
有谁知道如何将主题改为" Dark Nimbus"?
以下是相关代码:
/* 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>
答案 0 :(得分:7)
我最近也有同样的担忧,我从Geertjan Wielenga 和Neil C Smith找到了这些极好的提示。所以你的问题的答案是:
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
}
答案 1 :(得分:0)
代码在这种状态下运行得更好:
// 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!");
}