【发布时间】:2026-01-28 05:00:01
【问题描述】:
我有以下 Java 代码: 我有一个 for 循环,我正在创建 JButton。 我想返回单击按钮的索引。 所以我使用:
JButton jbtn = (JButton) e.getSource();
有什么方法可以返回 JButton 的索引吗?
我的代码如下:
for (int button = 0 ; button <= ListofJButtons.size() - 1; button++) {
ListofJButtons.get(button).addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("I clicked on button !");
JButton buttonSource = (JButton) e.getSource();
System.out.println( " sourceButton " + buttonSource.getIndex()); //is there any method like that in Java?
}
} );
}//for loop
是否可以获取点击的JButton的Index?
谢谢
【问题讨论】:
-
为什么?无关:请学习java命名约定并遵守它们
标签: java swing indexing jbutton