How to use an action listener to check if a certain button was clicked?

前端 未结 3 874
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 15:25

I have 4 lists of buttons arranged into a column in my program. As of now I have 4 loops that check to see if a button has been clicked or not. Is there a simple way to chec

3条回答
  •  醉梦人生
    2020-12-01 16:05

    Any time you click a button, it triggers the actionPerformed method, regardless of which button you pressed.

    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source instanceof JButton) System.out.println("You clicked a button!");
    }
    

提交回复
热议问题