What does ActionEvent e mean?

前端 未结 3 1465
走了就别回头了
走了就别回头了 2021-01-13 20:30

I am learning Java and would really like to have a deeper understanding of what the ActionEvent e perameter means and stands for. When I code I don\'t just want to spit out

3条回答
  •  鱼传尺愫
    2021-01-13 21:02

    This should help you: http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

    Basically,ButtonListener is your ActionListener Implementation. You will use it like

    someButton1.addActionListener(new ButtonListener());
    someButton2.addActionListener(new ButtonListener());
    

    It will listen for any action events on the buttons 'someButton1' and 'someButton2'.But we might want to handle clicks on both buttons in a different way. Thats when ActionEvent is of use.

    Inside method,we can do this by following

    @Override
    public void actionPerformed(ActionEvent e){
        if(e.getActionCommand().equals("Button 1")){
            //Process Button 1 action event here 
        }
        else if(e.getActionCommand().equals("Button 2")){
            //Process Button 2 action event here 
        }
    
    }
    

提交回复
热议问题