Java:外观 [英] Java: Look and Feel

查看:71
本文介绍了Java:外观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows机器上使用Netbeans,发生的情况是,如果运行主Java文件,则得到的外观与运行整个程序的情况有所不同. 意思是如果我这样做:

I am using Netbeans on a Windows machine, what happens is that if I run the main java file the look and feel I get is different than in the case I run the whole program. Meaning if I do this:

我知道

但是如果我这样做

我得到

您看到两个Java输出在外观上的差异了吗?为什么在那儿?我希望当我将其导出到Jar时,它应该像第一种情况一样打开,外观漂亮.怎么样?

Did you see the difference in the look and feel of both the java outputs? Why is it there? I want that when I export it to Jar it should open like the 1st case, nice looking buttons. How?

更新1: 我在主程序的开头找到了以下代码:

Update 1: I found the following code in the starting of my main:

public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormTTS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }

    //Some code here...
}

因此它应该设置雨云器的外观,但问题是当我打开广口瓶时,雨云器的外观和感觉就不存在了

So it should set the nimbus look and feel but the problem is that when I open the jar the Nimbus look and feel is not there

更新2:我的导航器

更新3:文件结构

推荐答案

我正在Windows计算机上使用Netbeans,如果我 运行主要的Java文件,我得到的外观与中的有所不同 这样我就可以运行整个程序.

I am using Netbeans on a Windows machine, what happens is that if I run the main java file the look and feel I get is different than in the case I run the whole program.

运行Swing应用程序时,默认外观将设置为跨平台L& F,也称为 JFrame 新建文件向导中的a>,它还包括一个main方法,仅用于测试目的,使开发人员能够运行"顶层容器.在此main方法中,外观设置为 Nimbus 更新1 中包含的a>.

When you run a Swing application the default Look and Feel is set to a cross-platform L&F also called Metal. On the other hand when you create a new JFrame from NetBeans New file wizard it also includes a main method just for test purposes, making developers able to "run" the top-level container. Within this main method the Look and Feel is set to Nimbus as you have included in your Update 1.

在此问答中对此进行了很好的解释:

This is well explained in this Q&A: How can I change the default look and feel of Jframe? (Not theme of Netbeans). As stated there you can modify the template associated to JFrame form to set the L&F you wish. However be aware of this line:

Java应用程序仅需要一个main类,因此此仅测试的main 部署应用程序时应删除方法. [...] L&F应该只在启动时建立一次,而不是在每次启动时建立 顶级容器(JFrameJDialog ...).

A Java application only needs one main class so this test-only main methods should be deleted when you will deploy your application. [...] the L&F should be established only once at the start-up, not in every top-level container (JFrame, JDialog...).

您还可以查看以编程方式设置如何设置外观<<的外观 /a>文章.

You also might to take a look to Programatically Setting the Look and Feel of How to Set the Look and Feel article.

我只是不了解我只能使用测试的主要方法的一件事 需要删除,如果我删除它们,我的程序将如何正常运行?

I just did not understand one thing which test-only main methods do i need to delete and if i delete them how will my prg run properly?

Java应用程序必须只有一个main方法来启动执行.部署JAR时,在MANIFEST.MF文件中定义了具有main方法的类.因此,不需要在每个顶级容器(JFrameJDialog)中都使用main方法,这不是一个好习惯.

A Java application must have only one main method that inits the execution. The class which has this main method is defined within MANIFEST.MF file when you deploy your JAR. So, having a main method in each top-level container (JFrame or JDialog) is not needed and it's not a good practice.

但是,有时您不想运行整个应用程序来测试特定框架/对话框的工作方式.这就是NetBeans在JFrameJDialog创建时包括此main方法的原因.但是如上所述,当您部署JAR时,应删除那些额外的" main方法.

However sometimes you don't want to run the whole application to test how a particular frame/dialog works. That's why NetBeans includes this main method on JFrame or JDialog creation. But as stated above when you will deploy your JAR you should delete those "extra" main methods.

是的,在我创建新内容时,您已经给出了如何做的方法 jframe,但我已经有20个了

and yah, in that you have given how to do it when i create new jframes, but i already have 20s of them

Swing应用程序通常具有一个JFrame和多个JDialog.请查看此主题以获取更多详细信息:使用多个JFrame,良好/不良做法?

A Swing application tipically has a single JFrame and multiple JDialog's. Take a look to this topic for further details: The Use of Multiple JFrames, Good/Bad Practice?

无论如何,那里都是灵气,这就是我想要的,但这就是 不是开什么东西

And anyways it is nimbus in there and it is what i want, but that is not what is opening

您只需要在main类(运行整个应用程序时执行的类)中以编程方式将L& F设置为Nimbus.您可以在其中复制粘贴 Update 1 中包含的代码.

You just need to programatically set the L&F to Nimbus in your main class (the one that is executed when you run the whole application). You can copy-paste the code you've included in your Update 1 there.

在NetBeans中创建新项目时,它也会要求您创建一个主类.假设我创建了一个名为Test的新项目,它将要求我创建一个像这样的主类:

When you create a new project in NetBeans it ask you for create a main class too. Let's say I create a new project called Test, it will ask me for create a main class like this:

此生成的Test类将具有触发应用程序执行的main方法:

This generated Test class will have the main method that triggers the application execution:

在此主要方法中,您必须放入 Update 1 中包含的代码:

Within this main method you have to put the code you've included in your Update 1:

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        try {
                            javax.swing.UIManager.setLookAndFeel(info.getClassName());
                            break;
                        } catch (ClassNotFoundException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (InstantiationException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (IllegalAccessException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        } catch (UnsupportedLookAndFeelException ex) {
                            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }                                       
            }
        });
    }

}

然后,当您运行应用程序时,L& F将设置为Nimbus,以覆盖默认的跨平台L& F.从此以后,所有创建的Swing组件都将把Nimbus用作L& F.

Then when you run your application the L&F will be set to Nimbus overriding the default cross-platform L&F. Henceforth all created Swing components will have Nimbus as L&F.

注意: 查看全文

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