意思如果我这样做:
我得到
但如果我这样做
我得到
你看到了java输出的外观和感觉吗?为什么在那里我想要的是当我将它导出到Jar时,应该像第一种情况一样打开,看起来很漂亮的按钮.怎么样?
更新1:
我在主要的开始时找到了以下代码:
public static void main(String args[]) { 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 ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,null,ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE,ex); } //Some code here... }
所以应该设置nimbus的外观和感觉,但问题是当我打开瓶子Nimbus的外观和感觉不在那里
更新2:我的导航仪的外观
更新3:文件结构
@H_404_22@解决方法
I am using Netbeans on a Windows machine,what happens is that if I
run the main java file the look and feel I get is different than in
the case I run the whole program.
当您运行Swing应用程序时,默认的“外观和感觉”设置为跨平台L& F也称为Metal.另一方面,当您从NetBeans新建文件向导创建新的JFrame时,它还包括仅用于测试目的的主要方法,使开发人员能够“运行”顶级容器.在这个主要方法中,Look&Feel设置为Nimbus,就像您的更新1中包含的那样.
这在Q& A:How can I change the default look and feel of Jframe? (Not theme of Netbeans)中有很好的解释.如上所述,您可以修改与JFrame表单相关联的模板,以设置您想要的L& F.但是请注意这一行:
A Java application only needs one
main
class so this test-onlymain
methods should be deleted when you will deploy your application. […]
the L&F should be established only once at the start-up,not in every
top-level container (JFrame
,JDialog
…).
您也可以查看Programatically Setting the Look and Feel Programatically Setting the Look and Feel的文章.
编辑
I just did not understand one thing which test-only main methods do i
need to delete and if i delete them how will my prg run properly?
一个Java应用程序必须只有一个main方法可以执行.当您部署JAR时,具有此主方法的类在MANIFEST.MF文件中定义.所以,不需要在每个顶层容器(JFrame或JDialog)中使用一个main方法,这不是一个好习惯.
但有时您不希望运行整个应用程序来测试特定帧/对话框的工作原理.这就是为什么NetBeans在JFrame或JDialog创建中包含这个主要方法.但是如上所述,当您部署JAR时,您应该删除这些“额外”的主要方法.
and yah,in that you have given how to do it when i create new
jframes,but i already have 20s of them
Swing应用程序具有单个JFrame和多个JDialog.有关详细信息,请查看此主题:The Use of Multiple JFrames,Good/Bad Practice?
And anyways it is nimbus in there and it is what i want,but that is
not what is opening
您只需要在主类(在运行整个应用程序时执行的L& F)编程设置为Nimbus.您可以将包含在更新1中的代码复制粘贴到那里.
编辑2
当您在NetBeans中创建一个新项目时,它会要求您创建一个主类.假设我创建了一个名为Test的新项目,它将要求我创建一个这样的主类:
public class Test { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { try { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } catch (ClassNotFoundException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } catch (InstantiationException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } catch (IllegalAccessException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } catch (UnsupportedLookAndFeelException ex) { Logger.getLogger(Test.class.getName()).log(Level.SEVERE,ex); } } } } }); } }
然后当您运行应用程序时,L& F将被设置为Nimbus覆盖默认的跨平台L& F.从此以后,所有创建的Swing组件都将Nimbus作为L& F.
注意:SwingUtilities.invokeLater()的电话原因在Initial Threads文章中解释.
@H_404_22@ @H_404_22@