簡體   English   中英

Java - 如何允許類的所有方法訪問在構造函數中初始化的數組?

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

我很想知道如何在一個類中聲明和初始化一個 JButton 數組,以便同一個類的構造函數和方法都可以訪問這個數組。

目前,我在類定義的開頭聲明了 JButton 和數組,並在類構造函數中初始化了數組。 但是,這不允許類的其余方法訪問數組的變量。

非常感謝您的幫助!

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

}

在類作用域中聲明並在構造函數中初始化的變量應該在類中的任何地方都可以訪問。

如:

private/public <Class> <object>;

應該足夠了,如果您需要進一步的幫助,則需要查看一些代碼。

[編輯] 您發布的內容應該可以正常工作,但在此之前

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

你應該聲明它們。 IE

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

否則你會得到一個 nullPointerException。 就可訪問性而言,您應該對自己擁有的東西感到滿意。

就訪問而言,這應該有效。 當 actionPerformed 被調用時,你應該得到一個錯誤,因為你嘗試訪問 demoArray[3],但它只從 0 到 2。

這是一些工作代碼,取自您自己的。 有一些變化,我已經評論過了。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM