Search code examples
javaswingwhile-loopactionlistenerjtextfield

Java Actionlistener submit buttons


When I input something into the textbox my application freezes and stops responding if I hit enter. Its a game where you try to gets a number between 1 and 100. I think I need a submit button or an ActionListener on the enter key. Can somebody help me with this. Also is there a way to break lines in FlowLayout? Or is another layout better? Here's my code:

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class game extends JFrame {

    private static final long serialVersionUID = 1L;
    private Random rand = new Random();
    private int number = rand.nextInt(101);
    private JLabel result, prompt;
    private JTextField input;


    game() {
        super("Numbers");
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        prompt = new JLabel("Enter a number between 1 and 100");
        add(prompt);

        input = new JTextField(3);
        add(input);

        result = new JLabel("");
        add(result);



        event a = new event();
        input.addActionListener(a);

    }

    public class event implements ActionListener {
        public void actionPerformed(ActionEvent a) {
            int guess = 0;

            try {
                guess = Integer.parseInt(input.getText());
            } catch(NumberFormatException e) {
                result.setText("Error - Illegal Data Entered");
                result.setForeground(Color.RED);
            }


            while(guess!=number) {
                if(guess>number) {
                    result.setText("Guess Too High. Try Again.");
                    result.setForeground(Color.CYAN);
                } else if(guess<number) {
                    result.setText("Guess Too Low. Try Again.");
                    result.setForeground(Color.CYAN);
                } else {
                    result.setText("Unknown Error");
                    result.setForeground(Color.RED);
                }
            }
            if(guess==number){
                result.setText("Right the number was "+number);
                result.setForeground(Color.GREEN);
            }
        }
    }

    public static void main(String[] args) {
        new game().setVisible(true);

    }

}

Thanks in advance.

Update

So I removed the While-loop and everything worked OK. Did it do that right?


Solution

  • Your problem is here, guess or number never changes the value, infinite loop

     while(guess!=number) {
                        if(guess>number) {
                            result.setText("Guess Too High. Try Again.");
                            result.setForeground(Color.CYAN);
                        } else if(guess<number) {
                            result.setText("Guess Too Low. Try Again.");
                            result.setForeground(Color.CYAN);
                        } else {
                            result.setText("Unknown Error");
                            result.setForeground(Color.RED);
                        }
                    }
    

    You only have to delete the while, is unnecesary

    So ur inner class would be like this

    public class MyEvent implements ActionListener {
            public void actionPerformed(ActionEvent a) {
                int guess = 0;
    
                try {
    
                   guess = Integer.parseInt(input.getText());
                            if(guess>number) {
                                result.setText("Guess Too High. Try Again.");
                                result.setForeground(Color.CYAN);
                            } else if(guess<number) {
                                result.setText("Guess Too Low. Try Again.");
                                result.setForeground(Color.CYAN);
                            } else {
                               result.setText("Right the number was "+number);
                               result.setForeground(Color.GREEN);                      
                            }
    
                }catch(NumberFormatException nfe){
                    result.setText("Error - Illegal Data Entered");
                    result.setForeground(Color.RED);               
                }
    
      }
    }
    

    i try catch all the block cause when that integer fails u have to end ur execution of the event..

    NOTE Follow conventions, put declarative names and classes should start with UpperCase