简体   繁体   中英

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.

Currently, I declared the JButtons and array at the start of my class definition and initialized the array in the class constructor. 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

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

Otherwise you will get a 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.

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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