bluethundr12

Untitled

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