Advertisement
bluethundr12

Untitled

Dec 29th, 2020
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.SwingConstants;
  4. import javax.swing.JTextField;
  5. import javax.swing.JButton;
  6.  
  7. import java.awt.Font;
  8. import java.awt.Color;
  9. import java.awt.Dimension;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.ActionEvent;
  12.  
  13. public class GuessingGame extends JFrame {
  14.     private static final long serialVersionUID = 1L;
  15.     private JLabel lblOutput;
  16.     private int theNumber;
  17.     private JTextField txtGuess;
  18.     private int numberOfTries = 8;
  19.     private JButton btnPlayAgain;
  20.     private JButton btnGuess;
  21.    
  22.     public void checkGuess() {
  23.         btnPlayAgain.setVisible(false);
  24.         String guessText = txtGuess.getText();
  25.         txtGuess.setText("");// Empties the contents of the text field.
  26.         String message = "";
  27.         try {
  28.             int guess = Integer.parseInt(guessText);
  29.             if (numberOfTries == 0) {
  30.                 btnGuess.setVisible(false);
  31.                 txtGuess.setVisible(false);
  32.                 message = "You Lost! A new game has begun and you have 8 guesses remaining.";
  33.                 btnPlayAgain.setVisible(true);
  34.             }
  35.             else if (guess < theNumber) {
  36.                 message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
  37.             }
  38.             else if (guess > theNumber) {
  39.                 message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
  40.             }
  41.             else {
  42.                 btnGuess.setVisible(false);
  43.                 txtGuess.setVisible(false);
  44.                 message = guess + " is correct. Let's play again!";
  45.                 btnPlayAgain.setVisible(true);
  46.             }
  47.         } catch (Exception e) {
  48.             message = "Enter a whole number between 1 and 10.";
  49.         } finally {
  50.             lblOutput.setText(message);
  51.             txtGuess.requestFocus();
  52.             txtGuess.selectAll();
  53.            
  54.         }
  55.         decrementNumberOfTries();
  56.     }
  57.  
  58.     public void newGame() {
  59.         numberOfTries = 8;
  60.         theNumber = (int) (Math.random() * 10 + 1);
  61.     }
  62.    
  63.     public void decrementNumberOfTries() {
  64.         --numberOfTries;       
  65.     }
  66.  
  67.     public GuessingGame() {
  68.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  69.         setTitle("Tim's Hi-Lo Guessing Game");
  70.         getContentPane().setLayout(null);
  71.  
  72.         JLabel lblTitle = new JLabel("Tim's Hi-Lo Guessing Game");
  73.         lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
  74.         lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
  75.         lblTitle.setBounds(-10, 15, 436, 32);
  76.         getContentPane().add(lblTitle);
  77.  
  78.         JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 10:");
  79.         lblGuessANumber.setBackground(new Color(240, 240, 240));
  80.         lblGuessANumber.setHorizontalAlignment(SwingConstants.RIGHT);
  81.         lblGuessANumber.setBounds(83, 57, 215, 13);
  82.         getContentPane().add(lblGuessANumber);
  83.  
  84.         txtGuess = new JTextField();
  85.         txtGuess.addActionListener((ActionEvent e) -> {
  86.             checkGuess();
  87.         });
  88.         txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
  89.         txtGuess.setBounds(239, 85, 27, 19);
  90.         getContentPane().add(txtGuess);
  91.         txtGuess.setColumns(10);
  92.  
  93.         JButton btnGuess = new JButton("Guess!");
  94.         btnGuess.addActionListener(new ActionListener() {
  95.             public void actionPerformed(ActionEvent e) {
  96.                 checkGuess();
  97.             }
  98.         });
  99.         btnGuess.setBounds(146, 84, 85, 21);
  100.         getContentPane().add(btnGuess);
  101.  
  102.         lblOutput = new JLabel("Enter a number above and click Guess!");
  103.         lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
  104.         lblOutput.setBounds(-40, 150, 500, 13);
  105.         getContentPane().add(lblOutput);
  106.        
  107.         btnPlayAgain = new JButton("Play Again!");
  108.         btnPlayAgain.addActionListener(new ActionListener() {
  109.             public void actionPerformed(ActionEvent e) {
  110.                 newGame();
  111.             }
  112.         });
  113.         btnPlayAgain.setBounds(146, 115, 141, 21);
  114.         getContentPane().add(btnPlayAgain);
  115.         btnPlayAgain.setVisible(false);
  116.        
  117.     }
  118.  
  119.     public static void main(String[] args) {
  120.         GuessingGame theGame = new GuessingGame();
  121.         theGame.newGame();
  122.         theGame.setSize(new Dimension(450, 300));
  123.         theGame.setVisible(true);
  124.  
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement