vendredi 5 avril 2019

If statement doesn't update

I am learning java and i got stuck. I am using JTextField as userInput. After the user presses enter i want my if statement to print for me "text got changed". Afeter that i want to set 2 getters. First for my boolean and 2nd a string that contains user answer.

import java.awt.*;  
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class setTextField{
    boolean gotChanged = false;
    String userInput;
    public void setTextField()
    {        
        Font fieldFont = new Font("Arial", Font.PLAIN, 20);
        JTextField test;
        test = new JTextField("Input here...");
        test.setBounds(widthPosition, 145, 220, 25);
        test.setFont(fieldFont);
        test.setBackground(Color.WHITE);
        test.setForeground(Color.GRAY.brighter());
        test.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                gotChanged = true;
                userInput = test.getText();
            }
        });
        if(gotChanged)
        {
            System.out.println("Your answer is " + test);
        }
    }
    public boolean getInfo()
    {
        return gotChanged;
    }    
    public String getInput()
    {
        return userInput;
    }
}

Aucun commentaire:

Enregistrer un commentaire