End Game After Last Round

Learn to show, update the highest score, and end the game after the last round.

Ending the round

The next step needed for the game is to be able to end a round and add the new points to the score.

  1. In initGUI(), add an action listener to endRoundButton to call endRound() when the button is clicked. Use JButton’s addActionListener() method, with a new ActionListener object as the parameter value. In the ActionListener, add a public method called actionPerformed(). The actionPerformed() method should take ActionEvent as a parameter and return nothing. In actionPerformed(), call endRound().

Note: Do not try to execute the code yet. It will have a syntax error because endRound() does not exist yet.

    ...
 
  private void initGUI() {
    
      ...
 
    JButton endRoundButton = new JButton("End Round");
    endRoundButton.addActionListener(__________________ {
      _____________ actionPerformed(_____________) {
        __________();
      }
    });
    buttonPanel.add(endRoundButton);
 
      ...

Get hands-on with 1200+ tech skills courses.