M
M
Makaron30002021-02-26 18:01:52
Java
Makaron3000, 2021-02-26 18:01:52

Why is the PlayAgain button not working?

Everyone knows the game "Guess the number". The problem is this: when the player guesses the number, the Play Again button appears, which should restart the game and disappear again, but it neither restarts the game nor disappears.

import javax.swing.JFrame;
import java.awt.Window.Type;
import javax.swing.JLabel;

import java.awt.Dimension;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class GuesstingGame extends JFrame {
  private JTextField txtGuess;
  private JLabel lblOutput;
  private int theNumber;
  private JButton btnPlayAgain;
  
  public void checkGuess() { //Own method
    String guessText = txtGuess.getText();
    String message = "";
    int guess = Integer.parseInt(guessText);
    
    if (guess < theNumber)
      message = guess + " is too low. Try again.";
    else if (guess > theNumber)
      message = guess + " is too high. Try again.";
    else {
      message = guess + " is correct. You win!.";
          btnPlayAgain.setVisible(true);
    }
        
    lblOutput.setText(message);
  }
  
  public void newGame() {  //Own method
    theNumber = (int)(Math.random() * 100 + 1);
  }
  
  public GuesstingGame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Ваше имя Hi-Lo Guessting Game");
    getContentPane().setLayout(null);
    
    JLabel lblNewLabel = new JLabel("Ваше Имя Hi-Lo Guessing Game");
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
    lblNewLabel.setBounds(0, 0, 434, 21);
    getContentPane().add(lblNewLabel);
    
    JLabel lblNewLabel_1 = new JLabel("Guess a number between 1 and 100:");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
    lblNewLabel_1.setBounds(36, 45, 247, 21);
    getContentPane().add(lblNewLabel_1);
    
    txtGuess = new JTextField();
    txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
    txtGuess.setBounds(293, 47, 65, 19);
    getContentPane().add(txtGuess);
    txtGuess.setColumns(10);
    
    JButton btnGuess = new JButton("Guess!");
    btnGuess.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        checkGuess();
      }
    });
    btnGuess.setFont(new Font("Tahoma", Font.BOLD, 13));
    btnGuess.setBounds(168, 111, 98, 36);
    getContentPane().add(btnGuess);
    
    lblOutput = new JLabel("Enter a number above and click Guess!");
    lblOutput.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
    lblOutput.setBounds(0, 171, 434, 21);
    getContentPane().add(lblOutput);
    
    btnPlayAgain = new JButton("Play Again?");
    btnPlayAgain.setVisible(false);
    btnPlayAgain.setFont(new Font("Tahoma", Font.BOLD, 11));
    btnPlayAgain.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        newGame();
                                btnPlayAgain.setVisible(false);
      }
    });
    btnPlayAgain.setBounds(168, 203, 98, 36);
    getContentPane().add(btnPlayAgain);
  }

  public static void main(String[] args) {
    GuesstingGame theGame = new GuesstingGame();
    
    theGame.newGame();
    
    //Ключевые слова new Dimension() в круглых скобках метода
    //setSize() сообщают компилятору Java о создании объекта типа
    //Dimension с шириной 450 пикселей и высотой 300 пикселей
    theGame.setSize(new Dimension(450, 300));
    
    //Наконец, добавьте в метод main() следующую строку, чтобы
    //приложение появилось на экране:
    theGame.setVisible(true);
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-02-26
@Makaron3000

Here you have this code:

btnPlayAgain.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        newGame();
      }
    });

I have never written in swing, so I'll ask a counter question: where in the newGame () method are the instructions for the button to restart the game and disappear? I just see that the value of theNumber variable is assigned there...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question