Java - 如何区分两个或多个 JButton?
Java - How can I difference between two or more JButtons?
我实际上在做一个项目,我有一个 ArrayList
的 JButtons
添加了一个 ActionListener
。而且我不知道如何在我的 ActionPerformed
方法中区分它们。这些按钮具有相同的标题,但任何按钮都执行相同的操作。
在您的 actionPerformed 方法中执行如下操作:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jButton1){
//perform action when jButton1 clicked
}
if(e.getSource() == jButton2){
//perform action when jButton2 clicked
}
//So on and so forth
}
我实际上在做一个项目,我有一个 ArrayList
的 JButtons
添加了一个 ActionListener
。而且我不知道如何在我的 ActionPerformed
方法中区分它们。这些按钮具有相同的标题,但任何按钮都执行相同的操作。
在您的 actionPerformed 方法中执行如下操作:
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jButton1){
//perform action when jButton1 clicked
}
if(e.getSource() == jButton2){
//perform action when jButton2 clicked
}
//So on and so forth
}