Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.SwingConstants;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- import java.awt.Font;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class GuessingGame extends JFrame {
- private static final long serialVersionUID = 1L;
- private JLabel lblOutput;
- private int theNumber;
- private JTextField txtGuess;
- private int numberOfTries = 8;
- private JButton btnPlayAgain;
- private JButton btnGuess;
- public void checkGuess() {
- btnPlayAgain.setVisible(false);
- String guessText = txtGuess.getText();
- txtGuess.setText("");// Empties the contents of the text field.
- String message = "";
- try {
- int guess = Integer.parseInt(guessText);
- if (numberOfTries == 0) {
- btnGuess.setVisible(false);
- txtGuess.setVisible(false);
- message = "You Lost! A new game has begun and you have 8 guesses remaining.";
- btnPlayAgain.setVisible(true);
- }
- else if (guess < theNumber) {
- message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
- }
- else if (guess > theNumber) {
- message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
- }
- else {
- btnGuess.setVisible(false);
- txtGuess.setVisible(false);
- message = guess + " is correct. Let's play again!";
- btnPlayAgain.setVisible(true);
- }
- } catch (java.lang.NumberFormatException e) {
- message = "Enter a whole number between 1 and 10.";
- } finally {
- lblOutput.setText(message);
- txtGuess.requestFocus();
- txtGuess.selectAll();
- }
- decrementNumberOfTries();
- }
- public void newGame() {
- numberOfTries = 8;
- theNumber = (int) (Math.random() * 10 + 1);
- btnGuess.setVisible(true);
- txtGuess.setVisible(true);
- btnPlayAgain.setVisible(false);
- lblOutput.setText("Enter a number above and click Guess!");
- }
- public void decrementNumberOfTries() {
- --numberOfTries;
- }
- public GuessingGame() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setTitle("Tim's Hi-Lo Guessing Game");
- getContentPane().setLayout(null);
- JLabel lblTitle = new JLabel("Tim's Hi-Lo Guessing Game");
- lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
- lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
- lblTitle.setBounds(-10, 15, 436, 32);
- getContentPane().add(lblTitle);
- JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 10:");
- lblGuessANumber.setBackground(new Color(240, 240, 240));
- lblGuessANumber.setHorizontalAlignment(SwingConstants.RIGHT);
- lblGuessANumber.setBounds(83, 57, 215, 13);
- getContentPane().add(lblGuessANumber);
- txtGuess = new JTextField();
- txtGuess.addActionListener((ActionEvent e) -> {
- checkGuess();
- });
- txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
- txtGuess.setBounds(239, 85, 27, 19);
- getContentPane().add(txtGuess);
- txtGuess.setColumns(10);
- this.btnGuess = new JButton("Guess!");
- btnGuess.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- checkGuess();
- }
- });
- btnGuess.setBounds(146, 84, 85, 21);
- getContentPane().add(btnGuess);
- lblOutput = new JLabel("Enter a number above and click Guess!");
- lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
- lblOutput.setBounds(-40, 150, 500, 13);
- getContentPane().add(lblOutput);
- btnPlayAgain = new JButton("Play Again!");
- btnPlayAgain.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- newGame();
- }
- });
- btnPlayAgain.setBounds(146, 115, 141, 21);
- getContentPane().add(btnPlayAgain);
- btnPlayAgain.setVisible(false);
- }
- public static void main(String[] args) {
- GuessingGame theGame = new GuessingGame();
- theGame.newGame();
- theGame.setSize(new Dimension(450, 300));
- theGame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement