JavaSwing中单击的JButton的返回索引

JavaSwing中单击的JButton的返回索引,java,swing,indexing,jbutton,Java,Swing,Indexing,Jbutton,我有以下Java代码: 我有一个for循环,我正在创建JButtons。 我想返回单击按钮的索引。 因此,我使用: JButton jbtn = (JButton) e.getSource(); 有没有返回JButton索引的方法 我的代码如下: for (int button = 0 ; button <= ListofJButtons.size() - 1; button++) { ListofJButtons.get(button).addActionListen

我有以下Java代码: 我有一个for循环,我正在创建JButtons。 我想返回单击按钮的索引。 因此,我使用:

    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

对于(int-button=0;button有几种方法。最简单的方法是:

for (int button = 0 ; button <= ListofJButtons.size() - 1; button++) {
    final int index = button;
    ListofJButtons.get(button).addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // Because index is final I can access it here and know the index
            System.out.println("I clicked on button "+index+"!");
            JButton buttonSource = (JButton) e.getSource();
            System.out.println( " sourceButton " + buttonSource.getIndex()); //is there any method like that in Java?
        }
    } );
}//for loop

对于(int-button=0;button有几种方法。最简单的方法是:

for (int button = 0 ; button <= ListofJButtons.size() - 1; button++) {
    final int index = button;
    ListofJButtons.get(button).addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // Because index is final I can access it here and know the index
            System.out.println("I clicked on button "+index+"!");
            JButton buttonSource = (JButton) e.getSource();
            System.out.println( " sourceButton " + buttonSource.getIndex()); //is there any method like that in Java?
        }
    } );
}//for loop

对于(int-button=0;button有几种方法。最简单的方法是:

for (int button = 0 ; button <= ListofJButtons.size() - 1; button++) {
    final int index = button;
    ListofJButtons.get(button).addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // Because index is final I can access it here and know the index
            System.out.println("I clicked on button "+index+"!");
            JButton buttonSource = (JButton) e.getSource();
            System.out.println( " sourceButton " + buttonSource.getIndex()); //is there any method like that in Java?
        }
    } );
}//for loop

对于(int-button=0;button有几种方法。最简单的方法是:

for (int button = 0 ; button <= ListofJButtons.size() - 1; button++) {
    final int index = button;
    ListofJButtons.get(button).addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // Because index is final I can access it here and know the index
            System.out.println("I clicked on button "+index+"!");
            JButton buttonSource = (JButton) e.getSource();
            System.out.println( " sourceButton " + buttonSource.getIndex()); //is there any method like that in Java?
        }
    } );
}//for loop

for(int-button=0;button我猜
ListofJButtons
是一个
java.util.List
。如果是这样,您可以使用

JButton buttonSource = (JButton) e.getSource();
int index = ListofJButtons.indexOf(buttonSource);

我猜
ListofJButtons
是一个
java.util.List

JButton buttonSource = (JButton) e.getSource();
int index = ListofJButtons.indexOf(buttonSource);

我猜
ListofJButtons
是一个
java.util.List

JButton buttonSource = (JButton) e.getSource();
int index = ListofJButtons.indexOf(buttonSource);

我猜
ListofJButtons
是一个
java.util.List

JButton buttonSource = (JButton) e.getSource();
int index = ListofJButtons.indexOf(buttonSource);

从ActionEvent可以执行getActionCommand,从button可以获取getText或getLabel。由于按钮没有任何属性为其提供索引,因此无法获取索引。未为其定义索引。

从ActionEvent可以执行getActionCommand,从button可以获取getText或getLabel。因为按钮没有任何为它们提供索引的属性都无法获取索引。中的索引未为它们定义。

从ActionEvent可以执行getActionCommand或从按钮可以获取getText或getLabel。由于按钮没有任何为它们提供索引的属性,因此无法获取索引。中的索引未为它们定义。

从ActionEvent可以o getActionCommand或from按钮您可以获取getText或getLabel。由于按钮没有任何属性为其提供索引,因此您无法获取索引。中的索引未为其定义。

为什么?无关:请学习java命名约定并坚持为什么?无关:请学习java命名约定并坚持为什么?无关:请学习java命名约定并坚持到底为什么?无关:请学习java命名约定并坚持到底