我应该在我的应用程序final中声明Swing组件吗? [英] Should I declare swing components in my application final..?

查看:58
本文介绍了我应该在我的应用程序final中声明Swing组件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有很多摇摆组件.我应该在我的类中将这些组件变量定为最终变量,然后在构造函数中实例化,还是应该直接实例化它们而不将其定为最终变量.

 公共类MyClass{私有最终JFrame firstFrame;私有最终JPanel firstPanel;私有最终JButton firstButton;公共MyClass(){firstFrame = new JFrame();firstPanel =新的JPanel();firstJButton =新的JButton();}} 

这种方法是考虑设计和优化的更好方法,还是下面的方法更好?

 公共类MyClass{公共无效createGUI(){私有JFrame firstFrame = new JFrame();私有JPanel firstPanel = new JPanel();私有JButton firstJButton = new JButton();}} 

解决方案

如果您不打算多次初始化摆动组件 ,则可以使用 final 修饰符可以使这一点变得清楚.这样可以防止重新分配组件.

您不能在方法范围内使用私有修饰符,方法范围内唯一允许的修饰符是 final 修饰符.例如:

 公共类MyClass{公共无效createGUI(){最终的JFrame firstFrame = new JFrame();最终的JPanel firstPanel = new JPanel();最后的JButton firstJButton = new JButton();}} 

但是,我更喜欢以下示例:

 公共类MyClass{私有最终JFrame firstFrame;私有最终JPanel firstPanel;私有最终JButton firstButton;公共MyClass(){firstFrame = new JFrame();firstPanel =新的JPanel();firstJButton =新的JButton();}} 

这将确保不能重新分配组件,并且还提供了对组件的实例范围访问,而在第一个示例中,范围限于方法 createGUI()的范围./p>

希望这会有所帮助.

In my application i have many swing components. Should i make these component variables final in my class and then instantiate in constructor or should i directly instantiate them without making them final.

public class MyClass 
{
    private final JFrame firstFrame;
    private final JPanel firstPanel;
    private final JButton firstButton;

    public MyClass()
    {
        firstFrame = new JFrame();
        firstPanel = new JPanel();
        firstJButton = new JButton();
    }
}

is this approach better taking into consideration design and optimization or the below one is better.

public class MyClass
{
    public void createGUI()
    {
        private JFrame firstFrame = new JFrame();
        private JPanel firstPanel = new JPanel();
        private JButton firstJButton = new JButton();
    }
}

解决方案

If you don't intend on initializing your swing components more than once, then you can use the final modifier to make this clear. This will prevent the components from being reassigned.

You can't have a private modifier within the scope of a method, the only modifier permitted in method scope is the final modifier. For example:

public class MyClass
{
    public void createGUI()
    {
        final JFrame firstFrame = new JFrame();
        final JPanel firstPanel = new JPanel();
        final JButton firstJButton = new JButton();
    }
}

However, I prefer the example:

public class MyClass 
{
    private final JFrame firstFrame;
    private final JPanel firstPanel;
    private final JButton firstButton;

    public MyClass()
    {
        firstFrame = new JFrame();
        firstPanel = new JPanel();
        firstJButton = new JButton();
    }
}

This would ensure that the components cannot be reassigned and also provides instance-scope access to the components whereas in the first example, the scope is limited to the scope of the method createGUI().

Hope this helps.

这篇关于我应该在我的应用程序final中声明Swing组件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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