如何更改Jframe的默认外观? (不是Netbeans的主题) [英] How can I change the default look and feel of Jframe? (Not theme of Netbeans)

查看:98
本文介绍了如何更改Jframe的默认外观? (不是Netbeans的主题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改从现在开始我将创建的所有jframe表单的默认外观,而不必手动编辑我从中创建的每个jframe的每个外观代码 从雨云"到"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的外观,因此我不必遍历Source选项卡并将每个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.

推荐答案

首先查看一下以下主题:

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

注意:我不是要为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.

您要更改的内容称为模板:您可以通过 New File 向导创建的每个文件都有一个关联的模板.话虽如此,但NetBeans使开发人员能够更新/创建默认模板.转到工具->模板,然后查找 Swing GUI表单-> 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.

这篇关于如何更改Jframe的默认外观? (不是Netbeans的主题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆