从按下 JButton 的地方
From where the JButton was pressed
板上的所有图块都是按钮,每个类别都在单独的数组中。
如何在不检查每个数组中每个元素的 .getSource()
的情况下知道按钮在 ActionListener 中属于哪个类别?
我可以为每个类别创建单独的 ActionListener,但我还是必须通过数组来获取 type/color。
也许有一些我不知道的东西真的很有用。
为您创建一个带有 'color' 和 'type' 字段的 class 按钮,它还扩展了 JButton 并实现了 ActionListener、MouseListener 或两者...
public class YourButton extends JButton implements ActionListener, MouseListener { }
在其中创建构造函数 class 以实例化 YourButton 对象...
public class YourButton(string color, string type) { }
创建游戏板时,实例化 YourButton class...
YourButton yb = new YourButton('color', 'type');
这个 class 的每个实例都有自己的 ActionListener and/or MouseListener,因此您可以从 YourButton class 中处理事件,并且您会知道颜色和类型,因为字段将在您实例化 class 并创建对象时设置。
您也可以像对待 JButton 一样对待 YourButton,因为它是 JButton 的扩展 class。
板上的所有图块都是按钮,每个类别都在单独的数组中。
如何在不检查每个数组中每个元素的 .getSource()
的情况下知道按钮在 ActionListener 中属于哪个类别?
我可以为每个类别创建单独的 ActionListener,但我还是必须通过数组来获取 type/color。
也许有一些我不知道的东西真的很有用。
为您创建一个带有 'color' 和 'type' 字段的 class 按钮,它还扩展了 JButton 并实现了 ActionListener、MouseListener 或两者...
public class YourButton extends JButton implements ActionListener, MouseListener { }
在其中创建构造函数 class 以实例化 YourButton 对象...
public class YourButton(string color, string type) { }
创建游戏板时,实例化 YourButton class...
YourButton yb = new YourButton('color', 'type');
这个 class 的每个实例都有自己的 ActionListener and/or MouseListener,因此您可以从 YourButton class 中处理事件,并且您会知道颜色和类型,因为字段将在您实例化 class 并创建对象时设置。
您也可以像对待 JButton 一样对待 YourButton,因为它是 JButton 的扩展 class。