bluethundr12

Untitled

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