bluethundr12

Create Button Action Listener Java

Dec 16th, 2020 (edited)
812
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 java.awt.Font;
  5. import javax.swing.JTextField;
  6. import java.awt.Color;
  7. import java.awt.Dimension;
  8.  
  9. import javax.swing.JButton;
  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.    
  20.     private void initialiseTextField(){//Initialises a textField to be used for all inputs.
  21.         txtGuess = new JTextField();
  22.         txtGuess.addActionListener(){
  23.             public void actionPerformed(ActionEvent e) {
  24.               checkGuess();
  25.             }
  26.         }
  27.     }
  28.  
  29.     public void checkGuess() {
  30.         String guessText = txtGuess.getText();
  31.         txtGuess.setText("");//Empties the contents of the text field.
  32.         String message = "";
  33.         int guess = Integer.parseInt(guessText);
  34.         if (guess < theNumber)
  35.             message = guess + " is too low. Try again.";
  36.         else if (guess > theNumber)
  37.             message = guess + " is too high. Try again.";
  38.         else {  
  39.             message = guess + " is correct. Let's play again!";
  40.             newGame();
  41.         }
  42.         lblOutput.setText(message);
  43.     }
  44.  
  45.     public void newGame() {
  46.         theNumber = (int) (Math.random() * 100 + 1);
  47.     }
  48.    
  49.     public GuessingGame() {
  50.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51.         setTitle("Tim's Hi-Lo Guessing Game");
  52.         getContentPane().setLayout(null);
  53.  
  54.         JLabel lblNewLabel = new JLabel("Tim's Hi-Lo Guessing Game");
  55.         lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15));
  56.         lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  57.         lblNewLabel.setBounds(0, 10, 436, 32);
  58.         getContentPane().add(lblNewLabel);
  59.  
  60.         JLabel lblNewLabel_1 = new JLabel("Guess a number between 1 and 100:");
  61.         lblNewLabel_1.setBackground(new Color(240, 240, 240));
  62.         lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
  63.         lblNewLabel_1.setBounds(147, 54, 215, 13);
  64.         getContentPane().add(lblNewLabel_1);
  65.  
  66.         txtGuess = new JTextField();
  67.         txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
  68.         txtGuess.setBounds(260, 122, 27, 19);
  69.         getContentPane().add(txtGuess);
  70.         txtGuess.setColumns(10);
  71.  
  72.         JButton btnGuess = new JButton("Guess!");
  73.         btnGuess.addActionListener(new ActionListener() {
  74.             public void actionPerformed(ActionEvent e) {
  75.                 checkGuess();
  76.             }
  77.         });
  78.         btnGuess.setBounds(172, 121, 85, 21);
  79.         getContentPane().add(btnGuess);
  80.  
  81.         lblOutput = new JLabel("Enter a number above and click Guess!");
  82.         lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
  83.         lblOutput.setBounds(58, 196, 350, 13);
  84.         getContentPane().add(lblOutput);
  85.     }
  86.  
  87.     public static void main(String[] args) {
  88.         GuessingGame theGame = new GuessingGame();
  89.         theGame.newGame();
  90.         theGame.setSize(new Dimension(450, 300));
  91.         theGame.setVisible(true);
  92.  
  93.     }
  94.  
  95. }
RAW Paste Data