What is wrong with this Java GUI?

I was just starting to learn the Java GUI and ran into this problem during event handling. Here is the initial window

When I enter a number inside the text box, it should say whether the guessed number is higher, lower, or matches. If it does not match, it requests a different number. But the window just freezes . After entering data

I guess it gets into an infinite loop. Here is the code. Help me figure out where the problem is. Thanks.

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class RandomNumGame extends JFrame { private JLabel promptLabel, resultLabel, answerLabel; private int tries=1, randomNum, guessNum; private JButton button; private JTextField txt; private boolean guessed; public RandomNumGame() { setLayout(new FlowLayout()); promptLabel = new JLabel("Guess a number(1-1000): "); add(promptLabel); txt = new JTextField(7); add(txt); button = new JButton("Guess!"); add(button); resultLabel = new JLabel(""); add(resultLabel); /*answerLabel = new JLabel(""); add(answerLabel); */ Event e = new Event(); button.addActionListener(e); } private class Event implements ActionListener{ public void actionPerformed(ActionEvent e){ randomNum = (int )(Math.random() * 1000 + 1); guessed=false; do{ try{ guessNum = (int)(Double.parseDouble(txt.getText())); if(guessNum>randomNum){ resultLabel.setText("Your number is higher. Try Again"); } else if(guessNum<randomNum){ resultLabel.setText("Your number is lower. Try Again"); } else{ resultLabel.setText("Your number matched!"); guessed=true; } } catch(Exception ee){ resultLabel.setText("Enter a legit number. What are you stupid?"); } }while(!guessed); } } public static void main(String[] args) { // TODO Auto-generated method stub RandomNumGame ran = new RandomNumGame(); ran.setDefaultCloseOperation(EXIT_ON_CLOSE); ran.setSize(300, 120); //ran.pack(); ran.setVisible(true); ran.setTitle("Random Number Game"); } } 
+5
source share
5 answers

you don't need a loop in your actionPerformed method. Just let it run once and check if it was guessed right away. You just get stuck in the loop when the user entered the wrong number. To make it smoother, create a random number only when guessed is true and just fulfill the assumption and conditions once.

 // Change constructor public RandomNumGame() { ... guessed = true; // initialize to true to create a new number when you click } private class Event implements ActionListener{ public void actionPerformed(ActionEvent e){ if(guessed) { // If the number was guessed, on the next click you get a new random number randomNum = (int )(Math.random() * 1000 + 1); guessed = false; } try{ guessNum = (int)(Double.parseDouble(txt.getText())); if(guessNum>randomNum){ resultLabel.setText("Your number is higher. Try Again"); } else if(guessNum<randomNum){ resultLabel.setText("Your number is lower. Try Again"); } else{ resultLabel.setText("Your number matched! Click again for a new Number"); guessed=true; } } catch(Exception ee){ resultLabel.setText("Enter a legit number. What are you stupid?"); } } } 
+2
source

The GUI structure has its own event loop, and its event processing (including response to text input, button presses, etc.) will be blocked during user code execution. You want your own event handler to complete as quickly as possible.

Build it as each time you press the button, the current rating is evaluated, messages are displayed, and the handler ends. In the interval between button presses, the program supports counting guesses, guessing numbers, etc.

+2
source

You should not read what the user writes in a loop. Your event listener on the guessing button should simply check to see if the number is higher or equal once, and then stop. It will be called again each time the user clicks a button. The way this is happening right now, apart from other problems, may be that he checks it once and stays on the loop, checking forever until he is right. Its not practical and bad programming, because it will consume a lot of processor, being in such an active expectation.

+1
source

You are right, this is a cycle problem. If the first number you randomNum does not match randomNum , the guessed variable guessed never be set to true . I suggest you do the following:

  • Do not initialize randomNum inside the event handler. Do it in the constructor.
  • delete do{...} while(!guessed) . You don’t need a loop here, just pull out the logic that you have inside the loop.
+1
source

Your random number and text entry will be equal before the loop. I assume that the value will not go into another condition when the guessed becomes true. either you need to add a random number generation operator during the loop. i.e.

 do{ randomNum = (int )(Math.random() * 1000 + 1); //It required here try{ guessNum = (int)(Double.parseDouble(txt.getText())); if(guessNum>randomNum){ resultLabel.setText("Your number is higher. Try Again"); } else if(guessNum<randomNum){ resultLabel.setText("Your number is lower. Try Again"); } else{ resultLabel.setText("Your number matched!"); guessed=true; } } catch(Exception ee){ resultLabel.setText("Enter a legit number. What are you stupid?"); } }while(!guessed); 
0
source

Source: https://habr.com/ru/post/1240504/


All Articles