user2426434
user2426434

Reputation: 491

Java won't recognize defined variables?

In actionPerformed it seems that all variables (submit, msg, input) "cannot be resolved" according to Eclipse. In my experience (of which I have very little) this means that I have not defined the variables. But, as you can see in the code, I have defined them. Submit is a JButton, msg is a string, input is a JTextField.

package levels;

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

import java.util.*;

public class LevelOne extends JFrame implements ActionListener{

    private Object msg;

    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);

        //these aren't being called to actionPerformed
        JButton submit = new JButton("Check sentence");
        submit.addActionListener(this);
        setVisible(true);
        JTextField input = new JTextField("Ich spielen Golf.");
        input.setActionCommand("input");
        add(input);
        input.addActionListener(this);
        setVisible(true);

        String msg = ("Test successful");
    }   

    //this should be successfully locating and utilizing "submit", "input" and "msg", but it won't
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == submit) {
            msg = submit.getText();

            //!!  Display msg only **after** the user has pressed enter.
            input.setText(msg); 
        }

    }
}

I am aware some of my imports are unnecessary.

P.S., i'm making a small text adventure game for my German class

Upvotes: 0

Views: 2680

Answers (5)

Loïc
Loïc

Reputation: 591

You need to declare your variable outside the method 'One' like:

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

public void one(){
    // whatever goes there
}

public void actionPerformed(ActionEvent e) {
   // submit is now working 
   if (e.getSource() == submit) {
   }
}

Upvotes: 0

Arthur Dent
Arthur Dent

Reputation: 795

The variable submit is defined as a local variable in the method one().

It is not available in the method actionPerformed().

Upvotes: 0

WW.
WW.

Reputation: 24271

The scope of those variables is the one() method only. If you want them available to the whole class put them at the top next to msg.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

It's because the input and submit variables are not accessible in the actionPerformed method.

Make the input and submit variables class members, like this:

pubilc class LevelOne {
    private JTextField input = new JTextField("Ich spielen Golf.");
    private Object msg;
    private JButton submit = new JButton("Check sentence");


    public void one() { ... }

    public void actionPerformed(ActionEvent e) { ... }
}

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691665

You defined the variables as local variables in the method one(). By definition, local variables are... local. They're visible only in the block here they're defined. To be visible in one() and in actionPerformed(), they should be defined as fields of the class.

An alternative would be to use an anonymous inner class defined in the one() method to defined your action listener, but given that you don't master variables yet, you'd better leave anonymous classes for a bit later. Swing is a complex framework, and you should probably do some more basic programming exercises before doing Swing.

Upvotes: 3

Related Questions