2013-10-11 69 views
0

我正在尝试创建一个数字游戏,其中涉及创建存储在二维数组中的JLabels网格。如何访问存储在JLabels二维数组中的JLabel超类的方法

JLabels本身是通过创建一个NumberPanel类的对象来创建的,该类创建一个显示随机数的JLabel。

在通过2D数组创建对象的主类中,我需要能够访问名为“isClicked”的NumberPanel类的方法,以将布尔值设置为true或false。

我已经粘贴下面我的代码,如果任何人都可以给我一个手,我将不胜感激:)

**NumberPanel Class:** 

public class NumberPanel extends JLabel { 

    private boolean isClicked; 

    NumberPanel() { 
     //Constructor code 
    } 

    public void clicked(boolean b) { //Need to access this method 
     isClicked = b; 
    } 
} 

**Original class containing NumberPanel objects** 

public class NumberGameGui extends JPanel { 

    private JLabel[][] numberGrid; 

private void gridPopulator() { 

    for (int i = 0; i < 12; i++) 
     for (int j = 0; j < 9; j++) { 
      numberGrid[i][j] = new NumberPanel(); 
      add (numberGrid[i][j]); 
      numberGrid[i][j].addMouseListener (new PanelListener()); 
     } 

    **NumberGrid[1][1].isClicked(true);** //Want to access isClicked method 
} 

回答

0

试试这个:

((NumberPanel)numberGrid[1][1]).isClicked(true); 

或者使用NumberPanel作为静态类的阵列:

private NumberPanel[][] numberGrid; 
+0

非常感谢你,问题解决了! – Unl1ght

相关问题