简体   繁体   English

Swing-GUI不知道在构造函数中实例化的外部类

[英]Swing-GUI doesn't know external class instantiated in constructor

I have a Swing-GUI and an external class. 我有一个Swing-GUI和一个外部类。 In the constructor of the Swing GUI I instantiate a new object of the external class. 在Swing GUI的构造函数中,我实例化了外部类的新对象。 But I can't use this object from other methods of the GUI-class (eg within an action listener). 但是我不能从GUI类的其他方法(例如,在动作侦听器中)使用此对象。 If I instantiate the object directly in the action listener then I can use all methods of the external class. 如果直接在动作侦听器中实例化对象,则可以使用外部类的所有方法。

Here are the relevant snippets of code; 以下是相关的代码片段; if you need more tell me :-) 如果您需要更多告诉我,:-)

1) my External class 1)我的外部课程

public class ExternalClass
{
    private int a = 100;
    public int getA() {
        return a;
    }
}

2) parts of my GUI-class 2)我的GUI类的一部分

public class GUI extends javax.swing.JFrame { 公共类GUI扩展了javax.swing.JFrame {

// constructor
public GUI()
{
    initComponents();
    ExternalClass e = new ExternalClass();
}
//...
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{

    int u = e.getA();
// this doesn't work - the object e is not known by the method

}
//...
        java.awt.EventQueue.invokeLater(new Runnable()
    {

        public void run()
        {
            new GUI().setVisible(true);

        }
    });

You declare and instantiate the e variable inside the constructor, so it is visible inside the constructor only. 您在构造函数内部声明和实例化e变量,因此它仅在构造函数内部可见。

Declare it as a member variable and you can either instantiate it there or in the constructor. 将其声明为成员变量,然后可以在其中或在构造函数中实例化它。

The scope of your e object (scope means how visible a variable is) is confined to the constructor, because you said ExternalClass e in that constructor. e对象的范围(范围表示变量的可见性)仅限于构造函数,因为您在该构造函数中说过ExternalClass e

The simplest solution would be to make this variable a member of the class - rather than just being defined in the constructor. 最简单的解决方案是使此变量成为类的成员-而不是仅在构造函数中进行定义。

Move the declaration of ExternalClass e outside the constructor, but still in the class itself. ExternalClass e的声明ExternalClass e构造函数之外,但仍移到类本身中。 In your constructor, just do e = new ExternalClass(); 在您的构造函数中,只需执行e = new ExternalClass(); . e is visible in the constructor here because the constructor is a lower scope than the class itself, and it will be visible in the jButton1ActionPerformed method too, for the same reason. e在这里的构造函数中可见,因为构造函数的作用域比类本身小,并且出于相同的原因,它在jButton1ActionPerformed方法中也是可见的。

The object is declared in the constructor. 该对象在构造函数中声明。 Thus, it only exists within the constructor itself. 因此,它仅存在于构造函数内部。 If you want to use it in other methods you must declare it outside, as an attribute of the class, eg like this. 如果要在其他方法中使用它,则必须在外部将其声明为类的属性,例如:

ExternalClass e;    

// constructor
public GUI()
{
    initComponents();
    e = new ExternalClass();
}

Notice that this field will be visible to all the classes in the package that contains your GUI class. 请注意,此字段对包含您的GUI类的包中的所有类都是可见的。 You might want to specify access level (private, public or none for package access). 您可能要指定访问级别(对于程序包访问,为私有,公共或无)。

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

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