How to set Action Listener to 3 buttons

后端 未结 3 864
执念已碎
执念已碎 2021-01-21 23:59

I\'m trying to make a stopwatch with three buttons, \"Start\", \"Pause\", and \"Stop\". My instructor only taught us how to set Action Listeners to two buttons. How do I set u

3条回答
  •  梦毁少年i
    2021-01-22 00:37

    All you need to add is this after the buttons creation.

    startButton.setActionCommand("Start");
    stopButton.setActionCommand("Stop");
    pauseButton.setActionCommand("Pause");
    

    and in actionPerformed method use this.

    switch(actionEvent.getActionCommand())
    {
    // cases
    }
    

提交回复
热议问题