user2426434
user2426434

Reputation: 491

What about this isn't allowing me to use my button or compare strings?

My goal is to compare two strings. One string is just the input from a user from a textfield (txt), and then, if they match, to change the textfield to a third string (msg).

However, when I enter the correct characters for the txt string and click the button, nothing happens. Why isn't it changing to "Derk?", the msg string?

Code:

package levels;


import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class LevelOne extends JFrame implements ActionListener{

    private JTextField input = new JTextField("Ich spielen Golf.");     
    private JButton submit = new JButton("Check sentence");


    public void one(){

        setTitle("Conjugator");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("images/LevelOneBG.gif")));
        setLayout(new FlowLayout());

        JTextArea area = new JTextArea("You enter a castle. A Goblin demands you correct his sentences!");
        add(area);
        setVisible(true);

        JButton submit = new JButton("Check sentence");
        submit.addActionListener(this);
        add(submit);
        setVisible(true);
        JTextField input = new JTextField("Ich spielen Golf.");
        input.setActionCommand("input");
        add(input);
        input.addActionListener(this);
        setVisible(true);


    }   


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == submit) {

            String txt = (input.getText());
            String test = ("test");
            String msg = ("Derk?");

            if (txt.equals(test)){
                //after check
                input.setText(msg); 
            }
        }
    }
}

Upvotes: 0

Views: 57

Answers (2)

assylias
assylias

Reputation: 328598

The reason is that you have two JTextFields and two JButtons. For example for JTextfield, you have one defined as an instance variable of your class:

private JTextField input = new JTextField("Ich spielen Golf.");

and another one that you create within the one method:

JTextField input = new JTextField("Ich spielen Golf.");

Only the latter is added to your frame but you reference the former in your actionPerformed method.

The easy way to fix it: in your one method, remove those lines:

JTextField input = new JTextField("Ich spielen Golf.");
JButton submit = new JButton("Check sentence");

Upvotes: 1

Andrew Cumming
Andrew Cumming

Reputation: 965

The problem is you redefine the button submit in your one method. Within your one method remove the line

JButton submit = new JButton("Check sentence");

and the line

JTextField input = new JTextField("Ich spielen Golf.");

and it should work fine.

Upvotes: 2

Related Questions