bluethundr12

GuessingGame.java file

Dec 18th, 2020
767
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.  
  18.     private JTextField txtGuess;
  19.     private JTextField txtNumberOfTries;
  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 (guess < theNumber)
  28.                 message = guess + " is too low. Try again.";
  29.             else if (guess > theNumber)
  30.                 message = guess + " is too high. Try again.";
  31.             else {
  32.                 message = guess + " is correct. Let's play again!";
  33.                 newGame();
  34.             }
  35.         } catch (Exception e) {
  36.             message = "Enter a whole number between 1 and 100.";
  37.         } finally {
  38.             lblOutput.setText(message);
  39.             txtGuess.requestFocus();
  40.             txtGuess.selectAll();
  41.  
  42.         }
  43.     }
  44.  
  45.     public void newGame() {
  46.         theNumber = (int) (Math.random() * 100 + 1);
  47.         txtNumberOfTries.setText("");
  48.         //lblNumberOfTries.setText("");
  49.     }
  50.    
  51.     public void incrementNumberOfTries() {
  52.         numberOfTries = numberOfTries + 1;     
  53.     }
  54.  
  55.     public GuessingGame() {
  56.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57.         setTitle("Tim's Hi-Lo Guessing Game");
  58.         getContentPane().setLayout(null);
  59.  
  60.         JLabel lblTitle = new JLabel("Tim's Hi-Lo Guessing Game");
  61.         lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
  62.         lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
  63.         lblTitle.setBounds(0, 10, 436, 32);
  64.         getContentPane().add(lblTitle);
  65.  
  66.         JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100:");
  67.         lblGuessANumber.setBackground(new Color(240, 240, 240));
  68.         lblGuessANumber.setHorizontalAlignment(SwingConstants.RIGHT);
  69.         lblGuessANumber.setBounds(147, 54, 215, 13);
  70.         getContentPane().add(lblGuessANumber);
  71.  
  72.         txtGuess = new JTextField();
  73.         txtGuess.addActionListener((ActionEvent e) -> {
  74.             checkGuess();
  75.         });
  76.         txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
  77.         txtGuess.setBounds(260, 122, 27, 19);
  78.         getContentPane().add(txtGuess);
  79.         txtGuess.setColumns(10);
  80.  
  81.         JButton btnGuess = new JButton("Guess!");
  82.         btnGuess.addActionListener(new ActionListener() {
  83.             public void actionPerformed(ActionEvent e) {
  84.                 checkGuess();
  85.             }
  86.         });
  87.         btnGuess.setBounds(172, 121, 85, 21);
  88.         getContentPane().add(btnGuess);
  89.  
  90.         lblOutput = new JLabel("Enter a number above and click Guess!");
  91.         lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
  92.         lblOutput.setBounds(58, 196, 350, 13);
  93.         getContentPane().add(lblOutput);
  94.        
  95.         JLabel lblNumberOfTries = new JLabel("Number of tries:");
  96.         lblNumberOfTries.setBounds(136, 219, 90, 13);
  97.         getContentPane().add(lblNumberOfTries);
  98.        
  99.         txtNumberOfTries = new JTextField();
  100.         txtNumberOfTries.setBounds(223, 219, 42, 13);
  101.         getContentPane().add(txtNumberOfTries);
  102.         txtNumberOfTries.setColumns(10);
  103.     }
  104.  
  105.     public static void main(String[] args) {
  106.         GuessingGame theGame = new GuessingGame();
  107.         theGame.newGame();
  108.         theGame.setSize(new Dimension(450, 300));
  109.         theGame.setVisible(true);
  110.  
  111.     }
  112. }
RAW Paste Data