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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22066620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:22:22  来源:igfitidea点击:

How can I change the default look and feel of Jframe? (Not theme of Netbeans)

javaswingnetbeansjframe

提问by Exena

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 表单的默认外观,而不必手动编辑我从“Nimbus”到“Windows”创建的每个 jframe 的每个外观代码。

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 表单”后,我希望外观和感觉代码如下所示:

        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 的外观和感觉,因此我不必通过源选项卡并将 Nimbus 更改为 Windows 为我创建的每个 Jframe .

采纳答案by dic19

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

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

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 的外观和感觉,因此我不必通过源选项卡并将 Nimbus 更改为 Windows 为我创建的每个 Jframe .

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

您要更改的内容称为模板:您可以通过“新建文件”向导创建的每个文件都有一个关联的模板。话虽如此,NetBeans 使开发人员能够更新/创建默认模板。转到工具 -> 模板并查找Swing GUI 表单 -> JFrame

enter image description here

在此处输入图片说明

You have two options here:

您在这里有两个选择:

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

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

我会选择选项 2 只是为了保持原始模板不变..

enter image description here

在此处输入图片说明

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

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

enter image description here

在此处输入图片说明

Finally to create a new "custom" JFramejust find your template in Swing GUI Forms -> MyJFrameTemplateas shown below:

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

enter image description here

在此处输入图片说明



In addition

此外

Reading @Misgevolution's comment below I think there's something to be clarified. This auto-generated mainmethod is there just for test purposes making developers be able to "run" top-level containers. A Java application only needs one mainclass so this test-only mainmethods 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 的评论,我认为有一些需要澄清的地方。这种自动生成的main方法仅用于测试目的,使开发人员能够“运行”顶级容器。Java 应用程序只需要一个main类,因此main在部署应用程序时应删除此仅测试方法。正如其他答案中所建议的,L&F 应该只在启动时建立一次,而不是在每个顶级容器中。

回答by Avinash Nivangune

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 行,只需将“windows”替换为“Nimbus”并将此代码放在应用程序的主框架中,并以嵌套形式调用另一个框架,它将自动为所有形式应用 nimbus theam。

回答by Misgevolution

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

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

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

Where lnfNameis the LookAndFeel name and frameis your JFrameObject.

lnfNameLookAndFeel 名称在哪里,frame是您的JFrame对象。