2 つの JButton を持つ GUI があり、getSource メソッドを使用してどちらがイベントを発生させているかを知りたいです。問題の解決策をGoogle全体で探しましたが、見つけることができませんでした。これが私のコードです:
@Override
public void actionPerformed(ActionEvent event){
if (event.getSource() == btn1){
System.out.println("this should work");
}else if (event.getSource() == btn2){
System.out.println("this should work as well");
}
どちらのボタンを押しても何も印刷されません。これも試しました:
@Override
public void actionPerformed(ActionEvent event){
if ((JButton)event.getSource() == btn1){
System.out.println("this should work");
}else if ((JButton)event.getSource() == btn2){
System.out.println("this should work as well");
}
しかし、それもうまくいきませんでした。
誰かが何が悪いのか教えてもらえますか?