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

2022-01-24 00:00:00 netbeans java swing jframe

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

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'.

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

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".

我希望在单击新建 > Jframe 表单"后,外观代码看起来像这样:

I want the look and feel code to look like this right after I click 'New > 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;
            }
        }
    }

注意:我不是想为 Netbeans 本身设置主题,我只是希望我创建的 Jframe 在默认情况下具有 Windows 的外观和感觉,因此我不必通过源选项卡并将 Nimbus 更改为 Windows我创建的 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.

推荐答案

首先看一下这个话题:多个 JFrames 的使用,好/坏做法?

注意:我不是想为 Netbeans 本身设置主题,我只想要 Jframe我创建的默认窗口外观和感觉,所以我不必须通过源选项卡并将 Nimbus 更改为 Windows我创建的每个 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 使开发人员能够更新/创建默认模板.转到 Tools -> Templates 并寻找 Swing GUI Forms -> JFrame

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

你有两个选择:

  1. 在编辑器中打开模板并在其中进行修改.
  2. 创建一个重复的模板并修改最后一个.

我会选择选项 2,只是为了保持原始模板不被修改..

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

当您编辑模板时,只需修改这一行(或实际需要的任何内容):

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

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

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

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

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.

相关文章