简体   繁体   English

Java - 如何允许类的所有方法访问在构造函数中初始化的数组?

[英]Java - How to allow all methods of a class to access an array initialized in constructor?

I'm interested in knowing how I can declare and initialize an array of JButtons in a class so that the constructor and methods of that same class will all have access to this array.我很想知道如何在一个类中声明和初始化一个 JButton 数组,以便同一个类的构造函数和方法都可以访问这个数组。

Currently, I declared the JButtons and array at the start of my class definition and initialized the array in the class constructor.目前,我在类定义的开头声明了 JButton 和数组,并在类构造函数中初始化了数组。 However, this does not allow the rest of the class' methods to access the array's variables.但是,这不允许类的其余方法访问数组的变量。

Your help is greatly appreciated!非常感谢您的帮助!

public class demo{
    public JButton one;
    public JButton two;
    public JButton three;

    public JButton demoArray[] = new JButton[3];

    public demo(){
         demoArray[0] = one;
         demoArray[1] = two;
         demoArray[2] = three;
         ....
         }

    public void actionPerformed(ActionEvent e)
    {...
        for(int i=0; i<3; i++)
        {
             demoArray[i].setEnabled(false);
        }
    }

} }

A variable declared in the class scope and initialising in the constructor should be accessible everywhere within the class.在类作用域中声明并在构造函数中初始化的变量应该在类中的任何地方都可以访问。

as in:如:

private/public <Class> <object>;

should be sufficient, will need to see some code if you require further assistance.应该足够了,如果您需要进一步的帮助,则需要查看一些代码。

[edit] What you posted should work fine, but before you do [编辑] 您发布的内容应该可以正常工作,但在此之前

demoArray[0] = one;
demoArray[1] = two;
demoArray[2] = three;

you should declare them.你应该声明它们。 ie IE

one = new JButton();.......

Otherwise you will get a nullPointerException.否则你会得到一个 nullPointerException。 as far as accessibility goes, you should be ok with what you have.就可访问性而言,您应该对自己拥有的东西感到满意。

As far as access goes, this should work.就访问而言,这应该有效。 You should be getting an error when actionPerformed is called, because you try and access demoArray[3], but it only goes from 0 to 2.当 actionPerformed 被调用时,你应该得到一个错误,因为你尝试访问 demoArray[3],但它只从 0 到 2。

Here is some working code, taken from your own.这是一些工作代码,取自您自己的。 There are a few changes, which I've commented.有一些变化,我已经评论过了。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

// Extend JPanel so we can put the buttons somewhere
// Implement ActionListener so we can listen to them
public class Demo extends JPanel implements ActionListener {

    public JButton one;
    public JButton two;
    public JButton three;

    // This holds the same objects as above. You don't need both.
    public JButton demoArray[] = new JButton[3];

    // This is used to show the results
    public static void main(String[] args) {
        // Create our Demo
        Demo demo = new Demo();

        JFrame frame = new JFrame("Test");
        frame.add(demo);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    // Using uppercase for Class name and lower case for objects
    public Demo() {

        super();

        // Create our buttons            
        one = new JButton("one");
        two = new JButton("two");
        three = new JButton("three");

        // Put them in the array.
        // We could have just created them in the array directly.
        demoArray[0] = one;
        demoArray[1] = two;
        demoArray[2] = three;

        // Put the buttons inside this (the JPanel)
        // and listen to them
        for (JButton button : demoArray) {
            add(button);
            button.addActionListener(this);
        }
    }

    // What to do when we hear them
    @Override
    public void actionPerformed(ActionEvent e) {
        // There are only three buttons, not four
        // That is, demoArray[0], demoArray[1], 
        // and demoArray[2]
        for (int i = 0; i < 3; i++) {
            demoArray[i].setEnabled(false);
        }
    }
}

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

相关问题 如何访问在构造函数(JAVA)中初始化的对象? - how to access the Objects initialized inside constructor(JAVA)? 如何在类的其他方法中使用在构造函数中初始化的对象 - How to use the object initialized in a constructor in other methods of the class 如何允许用户访问类的方法 - How to allow the user to access methods of a class 如何访问java中类构造函数内的数组列表? - How do I access an array list inside of a class constructor in java? 如何在Java中访问此类中的方法 - How to access the methods in this class in Java 如何让参数化构造函数链中的派生类访问使用派生构造函数初始化的基类的字段 - How to let a derived class in a parameterized constructor chain access fields of the base class that are initialized using the derived constructor Java:在构造函数中初始化的修改和访问变量 - Java: Modify & access variable initialized in constructor (Java)将参数从构造函数传递给类的所有方法 - (Java) Pass a parameter from a constructor to all the methods of a class Java类成员未在构造函数中初始化? - Java class member not being initialized in constructor? 如何在java中访问另一个类的构造函数? - How to access constructor of another class in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM