[英]How can I access a method of a JLabel super class stored in a 2D array of JLabels
I am trying to make a numbers game which involves creating a grid of JLabels which are stored in a 2D array. 我正在尝试制作一个数字游戏,其中涉及创建一个JLabel网格,这些网格存储在2D数组中。
The JLabels themselves are established by creating an object of the NumberPanel class which creates a JLabel displaying a random number. 通过创建NumberPanel类的对象来建立JLabel本身,该对象创建一个显示随机数的JLabel。
In the main class which creates the objects via the 2D array I need to be able to access a method of the NumberPanel class called "isClicked" to set a boolean value to true or false. 在通过2D数组创建对象的主类中,我需要能够访问NumberPanel类的名为“ isClicked”的方法,以将布尔值设置为true或false。
I have pasted my code below, if anyone can give me a hand I would greatly appreciate it :) 我在下面粘贴了我的代码,如果有人可以帮我,我将不胜感激:)
**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
}
Try this: 尝试这个:
((NumberPanel)numberGrid[1][1]).isClicked(true);
Or use NumberPanel as the static class of the array: 或使用NumberPanel作为数组的静态类:
private NumberPanel[][] numberGrid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.