查找Java JButton数组的索引 [英] Find the index of a Java JButton Array

查看:137
本文介绍了查找Java JButton数组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java程序,其中有一个按钮数组(而不是计算器!),并且我正在寻找一种有效的方法来了解单击了哪个JButton.到目前为止,据我所知,Java的唯一方法是将它们全部放在同一个ActionListener中,然后循环查找匹配项.

I am writing a Java program in which I have an array of buttons (not a calculator!) and I'm looking for an efficient way to know which JButton was clicked. From what I know of Java so far, the only way to do it is to have them all in the same ActionListener and then loop through looking for a match.

我刚刚想到的另一种解决方案是扩展JButton以在构造函数中包含唯一的ID号变量.在instanceof检查之后将事件对象强制转换为JButton时,似乎应该可以使用.有点像使用分配给索引号的VB的Tag属性.

Another solution I just thought of would be to extend JButton to include a unique ID number variable in the constructor. It seems that should work when the event object is cast to JButton after an instanceof check. Kind of like using VB's Tag property which is assigned to the index number.

有没有更好/更优雅的方式?

Is there a better/more elegant way?

推荐答案

有没有更好/更优雅的方式?

Is there a better/more elegant way?

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆