简体   繁体   English

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

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

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默认情况下具有窗口的外观和感觉所以我不必通过源选项卡并将Nimbus更改为Windows我为每个创建的Jframe创建。

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默认情况下具有窗口的外观和感觉所以我不必通过源选项卡并将Nimbus更改为Windows我为每个创建的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. 说过这个NetBeans后,开发人员可以更新/创建默认模板。 Go to Tools -> Templates and look for Swing GUI Forms -> JFrame 转到工具 - >模板,然后查找Swing GUI表单 - > JFrame

在此输入图像描述

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. 创建一个重复的模板并修改最后一个。

I'd go with Option 2 just to keep the original template unmodified.. 我选择2只是为了保持原始模板不被修改。

在此输入图像描述

When you edit the template just modify this line (or watherever you want actually): 编辑模板时,只需修改此行(或实际需要的观察者):

在此输入图像描述

Finally to create a new "custom" JFrame just find your template in Swing GUI Forms -> MyJFrameTemplate as shown below: 最后创建一个新的“自定义” JFrame只需在Swing GUI Forms中找到您的模板- > MyJFrameTemplate ,如下所示:

在此输入图像描述


In addition 此外

Reading @Misgevolution's comment below I think there's something to be clarified. 阅读@Misgevolution在下面的评论我认为有一些事情需要澄清。 This auto-generated main method is there just for test purposes making developers be able to "run" top-level containers. 这个自动生成的main方法仅用于测试目的,使开发人员能够“运行”顶级容器。 A Java application only needs one main class so this test-only main methods should be deleted when you will deploy your application. Java应用程序只需要一个main类,因此在部署应用程序时应删除此仅测试main方法。 As suggested in other answers the L&F should be established only once at the start-up, not in every top-level container. 正如其他答案中所建议的那样,L&F应该只在启动时建立一次,而不是在每个顶级容器中建立。

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。

See Changing the Look and Feel After Startup section of the java tutorial . 请参阅java教程中的“ 更改启动后的外观”部分。 You have to call like: 你必须像这样打电话:

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

Where lnfName is the LookAndFeel name and frame is your JFrame Object. 其中lnfName是LookAndFeel名称, frame是您的JFrame对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM