Advertisement
thotfrnk

swingNumberGuess.java

Jun 14th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. // school work
  2. import java.awt.*; //using awt's layouts
  3. import java.awt.event.*; //using awt's event classes and listener interfaces
  4. import javax.swing.*; //using swing's components and container
  5.  
  6. //a swing application extends from javax.swing.JFrame
  7. public class SwingNumberGuess extends JFrame {
  8.     private JTextField tfNumber, tfGuess;
  9.     private JButton btnGenerate;
  10.     private double number;
  11.     private float guess;
  12.     private Label responseLabel;
  13.  
  14.     // Constructor to set up UI components and event handlers
  15.     public SwingNumberGuess() {
  16.         // Swing components should be added to the content-pane of the JFrame.
  17.         Container cp = getContentPane();
  18.         // Set this Container to grid layout of 2 rows and 1 columns
  19.         cp.setLayout(new GridLayout(2, 1, 10, 6));
  20.  
  21.         // Components are added from left-to-right, top-to-bottom
  22.         cp.add(new JLabel("The Number is "));
  23.         tfNumber = new JTextField(10);
  24.         tfNumber.setHorizontalAlignment(JTextField.RIGHT);
  25.         cp.add(tfNumber);
  26.  
  27.         btnGenerate = new JButton("Generate");
  28.         cp.add(btnGenerate);
  29.         btnGenerate.addActionListener(new ActionListener() {
  30.             @Override
  31.             public void actionPerformed(ActionEvent evt) {
  32.                 number= (Math.random());
  33.                 tfNumber.setText(String.format("%.2f",number ));
  34.                 tfNumber.setText("**");
  35.             }
  36.         });
  37.         cp.add(new JLabel("Enter your guess "));
  38.         tfGuess = new JTextField(10);
  39.         tfGuess.setHorizontalAlignment(JTextField.RIGHT);
  40.         cp.add(tfGuess);
  41.  
  42.  
  43.  
  44.         responseLabel = new Label();
  45.         responseLabel.setAlignment(Label.LEFT);
  46.         cp.add(responseLabel);
  47.         tfGuess.addActionListener(new ActionListener() {
  48.             @Override
  49.             public void actionPerformed(ActionEvent evt) {
  50.                 guess = Float.parseFloat(tfGuess.getText());
  51.  
  52.                 if ( guess > number) {
  53.                     responseLabel.setText("Try Lower");
  54.                 }
  55.  
  56.                 else
  57.                 if ( guess < number){
  58.                     responseLabel.setText("Try Higher");
  59.                 }
  60.  
  61.                 else
  62.                 if (guess == number) {
  63.                     responseLabel.setText("You Got It");
  64.                 }
  65.  
  66.             }
  67.         });
  68.  
  69.         setDefaultCloseOperation(EXIT_ON_CLOSE); // for the "window-close" button
  70.         setTitle("Guess Number");
  71.         setSize(500, 120);
  72.         setVisible(true);
  73.     }
  74.  
  75.  
  76.     // The entry main() method
  77.     public static void main(String[] args) {
  78.         // For thread safety, use the event-dispatching thread to construct UI
  79.         javax.swing.SwingUtilities.invokeLater(new Runnable() {
  80.             @Override
  81.             public void run() {
  82.                 new SwingNumberGuess(); // Let the constructor does the job
  83.             }
  84.         });
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement