Advertisement
Guest User

Fixed

a guest
Dec 16th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package stackoverflowtests;
  2.        
  3. import javax.swing.JFrame;
  4. import javax.swing.JLabel;
  5. import javax.swing.SwingConstants;
  6. import java.awt.Font;
  7. import javax.swing.JTextField;
  8. import java.awt.Color;
  9. import java.awt.Dimension;
  10.  
  11. import javax.swing.JButton;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.ActionEvent;
  14.  
  15. public class GuessingGame extends JFrame {
  16.     private static final long serialVersionUID = 1L;
  17.     private JLabel lblOutput;
  18.     private int theNumber;
  19.    
  20.  
  21.     private JTextField txtGuess;
  22.  
  23.  
  24.     public void checkGuess() {
  25.         String guessText = txtGuess.getText();
  26.         txtGuess.setText("");//Empties the contents of the text field.
  27.         String message = "";
  28.         int guess = Integer.parseInt(guessText);
  29.         if (guess < theNumber)
  30.             message = guess + " is too low. Try again.";
  31.         else if (guess > theNumber)
  32.             message = guess + " is too high. Try again.";
  33.         else {  
  34.             message = guess + " is correct. Let's play again!";
  35.             newGame();
  36.         }
  37.         lblOutput.setText(message);
  38.     }
  39.  
  40.     public void newGame() {
  41.         theNumber = (int) (Math.random() * 100 + 1);
  42.     }
  43.    
  44.     public GuessingGame() {
  45.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.         setTitle("Tim's Hi-Lo Guessing Game");
  47.         getContentPane().setLayout(null);
  48.  
  49.         JLabel lblNewLabel = new JLabel("Tim's Hi-Lo Guessing Game");
  50.         lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
  51.         lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  52.         lblNewLabel.setBounds(0, 10, 436, 32);
  53.         getContentPane().add(lblNewLabel);
  54.  
  55.         JLabel lblNewLabel_1 = new JLabel("Guess a number between 1 and 100:");
  56.         lblNewLabel_1.setBackground(new Color(240, 240, 240));
  57.         lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
  58.         lblNewLabel_1.setBounds(147, 54, 215, 13);
  59.         getContentPane().add(lblNewLabel_1);
  60.  
  61.         txtGuess = new JTextField();
  62.         txtGuess.addActionListener((ActionEvent e) -> {
  63.             checkGuess();
  64.         });
  65.         txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
  66.         txtGuess.setBounds(260, 122, 27, 19);
  67.         getContentPane().add(txtGuess);
  68.         txtGuess.setColumns(10);
  69.  
  70.         JButton btnGuess = new JButton("Guess!");
  71.         btnGuess.addActionListener((ActionEvent e) -> {
  72.             checkGuess();
  73.         });
  74.         btnGuess.setBounds(172, 121, 85, 21);
  75.         getContentPane().add(btnGuess);
  76.  
  77.         lblOutput = new JLabel("Enter a number above and click Guess!");
  78.         lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
  79.         lblOutput.setBounds(58, 196, 350, 13);
  80.         getContentPane().add(lblOutput);
  81.     }
  82.  
  83.     public static void main(String[] args) {
  84.         GuessingGame theGame = new GuessingGame();
  85.         theGame.newGame();
  86.         theGame.setSize(new Dimension(450, 300));
  87.         theGame.setVisible(true);
  88.  
  89.     }
  90.  
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement