Question

The user will input a guess, the program will check if it's larger or smaller than the randomly generated number and then loop again until the guess == number.

The gtf (guess text field) is where the user will input the guess and the ttf (times text field) is where the number of times the user have guessed is displayed.

The problem is that it just skips the if-statement (e.getSource() == gtf), it doesn't wait for the user to input a number into gtf. Can't you check twice for user action inside the actionPerformer? What other solutions could I use?

import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener{

    private int number, guess;
    private int time=0;
    private int result;
    private JButton btnStart;
    private JTextField ttf, gtf;
    private String msg;


//Constructor

public Test(){

    ttf = new JTextField();
    gtf = new JTextField();

    btnStart = new JButton("Start");
    btnStart.addActionListener(this);
    gtf.addActionListener(this);
    gtf.setEditable(false);    
    ttf.setEditable(false);
    msg = ("Guess a number: ");

    Container c = getContentPane();

    c.setLayout(new BorderLayout());
    c.setLayout(new GridLayout(3, 2));
    c.add(new JLabel("Click to start the test: ", JLabel.RIGHT));
    c.add(btnStart);
    JLabel msglabel = new JLabel(msg, JLabel.RIGHT);
    msglabel.setForeground(Color.red);
    c.add(msglabel);
    c.add(gtf);
    c.add(new JLabel("Number of guesses: ", JLabel.RIGHT));
    c.add(ttf);

    setSize(600, 100);
    setTitle("Test");
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e){

    if (e.getSource() == btnStart){

        gtf.setEditable(true);
        btnStart.setEnabled(false);

        Random generator = new Random();
        number = generator.nextInt(99) + 1;

        if (e.getSource() == gtf) {
        String g = gtf.getName();
        guess = Integer.parseInt(g);

        while(time<=9)
            {
                time++;
                ttf.setText(Integer.toString(time));

            if(guess>number)
                {
                   msg = (guess + "is too big. Try again: ");

                }   

           else if(guess<number)
                {
                   msg = (guess + " is too small. Try again: ");
                }   

           else if (guess==number)
           {
               break;
           }
       }


    result = (int) Math.ceil(time/3.0);

More code to print result...
Was it helpful?

Solution

Can't you check twice for user action inside the actionPerformer?

No, Swing is event-based so only one component can trigger the ActionEvent. This means that you will need to place

if (e.getSource() == gtf) {

at an outer level of the if statement to check the source component (which will be triggered by pressing ENTER)

OTHER TIPS

you don't need the if (e.getSource() == gtf) statement at all, becaus you start checking the input when the user clicks btnStart, so if you pass this statement if (e.getSource() == btnStart) then if (e.getSource() == gtf) will surely fail, so just remove it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top