如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

时间:2023-01-24 17:11:47

I want to change the default look and feel of all the jframe forms I'll create from here on out instead of having to manually edit every look and feel code of every jframe I create from 'Nimbus' to 'Windows'.

我想更改从这里开始创建的所有jframe表单的默认外观和感觉,而不是必须手动编辑我创建的每个jframe的每个外观和感觉代码,从'Nimbus'到'Windows'。

So what I want to happen is that from when I startup Netbeans to when I create a new Jframe, the code for the look and feel of that Jframe I just created will automatically be set to "Windows" rather than "Nimbus".

所以我想要发生的是,从我启动Netbeans到我创建一个新的Jframe,我刚刚创建的Jframe的外观和感觉的代码将自动设置为“Windows”而不是“Nimbus”。

I want the look and feel code to look like this right after I click 'New > Jframe form':

当我点击“新的> Jframe form”后,我希望外观代码看起来是这样的:

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

Note: I am not trying to theme Netbeans itself, I just want the Jframe that I create to have the windows look and feel by default so I don't have to go through the source tab and change Nimbus to Windows for every Jframe I create.

注意:我并不是在尝试主题Netbeans本身,我只是希望我创建的Jframe具有windows的默认外观和感觉,因此我不必通过source选项卡,并为我创建的每个Jframe将Nimbus更改为windows。

3 个解决方案

#1


17  

First of all take a look to this topic: The Use of Multiple JFrames, Good/Bad Practice?

首先看看这个主题:使用多个jframe,好/坏的实践?

Note: I am not trying to theme Netbeans itself, I just want the Jframe that I create to have the windows look and feel by default so I don't have to go through the source tab and change Nimbus to Windows for every Jframe I create.

注意:我并不是在尝试主题Netbeans本身,我只是希望我创建的Jframe具有windows的默认外观和感觉,因此我不必通过source选项卡,并为我创建的每个Jframe将Nimbus更改为windows。

What you want to change is called template: every file you can create through the New File wizard has an associated template. Having said this NetBeans gives the developers the ability to update/create default templates. Go to Tools -> Templates and look for Swing GUI Forms -> JFrame

您想要更改的是template:通过New file向导创建的每个文件都有一个关联的模板。已经说过这个NetBeans给了开发人员更新/创建默认模板的能力。转到Tools ->模板并查找Swing GUI表单-> JFrame

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

You have two options here:

你有两个选择:

  1. Open the template in the editor and modify it there.
  2. 在编辑器中打开模板并在那里修改它。
  3. Create a duplicate template and modify this last one.
  4. 创建一个复制模板并修改最后一个模板。

I'd go with Option 2 just to keep the original template unmodified..

我选择选项2只是为了不修改原始模板。

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

When you edit the template just modify this line (or watherever you want actually):

当你编辑模板时,只需修改这一行(或者你想要的任何东西):

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

Finally to create a new "custom" JFrame just find your template in Swing GUI Forms -> MyJFrameTemplate as shown below:

最后,要创建一个新的“自定义”JFrame,只需在Swing GUI表单中找到您的模板—> MyJFrameTemplate,如下所示:

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)


In addition

Reading @Misgevolution's comment below I think there's something to be clarified. This auto-generated main method is there just for test purposes making developers be able to "run" top-level containers. A Java application only needs one main class so this test-only main methods should be deleted when you will deploy your application. As suggested in other answers the L&F should be established only once at the start-up, not in every top-level container.

阅读下面@Misgevolution的评论,我认为有一些事情需要澄清。这个自动生成的主方法仅用于测试目的,使开发人员能够“运行”*容器。Java应用程序只需要一个主类,因此在部署应用程序时应该删除这个仅用于测试的主方法。正如其他答案所建议的,L&F应该只在启动时建立一次,而不是在每个*容器中。

#2


4  

1. try {
2.        for (javax.swing.UIManager.LookAndFeelInfo info :  javax.swing.UIManager.getInstalledLookAndFeels()) {
3.            if ("*Windows*".equals(info.getName())) {
4.               javax.swing.UIManager.setLookAndFeel(info.getClassName());
5.                break;
6.             }
7.         }
8.    }

at line '3' just replace "windows" by "Nimbus" and put this code in main frame of application and call another frames in nested form it will automatically apply nimbus theam for all the forms.

在第3行,用“Nimbus”替换“windows”,将此代码放到应用程序的主框架中,并在嵌套表单中调用另一个帧,它将自动应用Nimbus theam来实现所有表单。

#3


3  

See Changing the Look and Feel After Startup section of the java tutorial. You have to call like:

请参阅更改java教程的启动部分后的外观和感觉。你必须这样打电话:

UIManager.setLookAndFeel(lnfName);    
SwingUtilities.updateComponentTreeUI(frame);    
frame.pack();

Where lnfName is the LookAndFeel name and frame is your JFrame Object.

lnfName是LookAndFeel名称,frame是JFrame对象。

#1


17  

First of all take a look to this topic: The Use of Multiple JFrames, Good/Bad Practice?

首先看看这个主题:使用多个jframe,好/坏的实践?

Note: I am not trying to theme Netbeans itself, I just want the Jframe that I create to have the windows look and feel by default so I don't have to go through the source tab and change Nimbus to Windows for every Jframe I create.

注意:我并不是在尝试主题Netbeans本身,我只是希望我创建的Jframe具有windows的默认外观和感觉,因此我不必通过source选项卡,并为我创建的每个Jframe将Nimbus更改为windows。

What you want to change is called template: every file you can create through the New File wizard has an associated template. Having said this NetBeans gives the developers the ability to update/create default templates. Go to Tools -> Templates and look for Swing GUI Forms -> JFrame

您想要更改的是template:通过New file向导创建的每个文件都有一个关联的模板。已经说过这个NetBeans给了开发人员更新/创建默认模板的能力。转到Tools ->模板并查找Swing GUI表单-> JFrame

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

You have two options here:

你有两个选择:

  1. Open the template in the editor and modify it there.
  2. 在编辑器中打开模板并在那里修改它。
  3. Create a duplicate template and modify this last one.
  4. 创建一个复制模板并修改最后一个模板。

I'd go with Option 2 just to keep the original template unmodified..

我选择选项2只是为了不修改原始模板。

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

When you edit the template just modify this line (or watherever you want actually):

当你编辑模板时,只需修改这一行(或者你想要的任何东西):

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)

Finally to create a new "custom" JFrame just find your template in Swing GUI Forms -> MyJFrameTemplate as shown below:

最后,要创建一个新的“自定义”JFrame,只需在Swing GUI表单中找到您的模板—> MyJFrameTemplate,如下所示:

如何更改Jframe的默认外观和感觉?(不是Netbeans的主题)


In addition

Reading @Misgevolution's comment below I think there's something to be clarified. This auto-generated main method is there just for test purposes making developers be able to "run" top-level containers. A Java application only needs one main class so this test-only main methods should be deleted when you will deploy your application. As suggested in other answers the L&F should be established only once at the start-up, not in every top-level container.

阅读下面@Misgevolution的评论,我认为有一些事情需要澄清。这个自动生成的主方法仅用于测试目的,使开发人员能够“运行”*容器。Java应用程序只需要一个主类,因此在部署应用程序时应该删除这个仅用于测试的主方法。正如其他答案所建议的,L&F应该只在启动时建立一次,而不是在每个*容器中。

#2


4  

1. try {
2.        for (javax.swing.UIManager.LookAndFeelInfo info :  javax.swing.UIManager.getInstalledLookAndFeels()) {
3.            if ("*Windows*".equals(info.getName())) {
4.               javax.swing.UIManager.setLookAndFeel(info.getClassName());
5.                break;
6.             }
7.         }
8.    }

at line '3' just replace "windows" by "Nimbus" and put this code in main frame of application and call another frames in nested form it will automatically apply nimbus theam for all the forms.

在第3行,用“Nimbus”替换“windows”,将此代码放到应用程序的主框架中,并在嵌套表单中调用另一个帧,它将自动应用Nimbus theam来实现所有表单。

#3


3  

See Changing the Look and Feel After Startup section of the java tutorial. You have to call like:

请参阅更改java教程的启动部分后的外观和感觉。你必须这样打电话:

UIManager.setLookAndFeel(lnfName);    
SwingUtilities.updateComponentTreeUI(frame);    
frame.pack();

Where lnfName is the LookAndFeel name and frame is your JFrame Object.

lnfName是LookAndFeel名称,frame是JFrame对象。