Filip123go
Filip123go

Reputation: 395

Program doesn't recognize inner classes in java

I want to use 2 buttons on the same JFrame to do different task each. One to change the label on the right and one to change the color of the circle in the middle. (The random changing color is on another class.)

For some unknown reason the program doesn't seem to recognize the inner classes which exists inside the main class (class TwoButtons). I am pretty new to java and I cannot find what I am doing wrong.... Could you please help me to resolve my problem?

package twoButtonsPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TwoButtons {
    JFrame frame;
    JLabel label;


    public static void main(String[] args) {
        TwoButtons gui = new TwoButtons();
        gui.go();

    }
    public void go(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton labelButton = new JButton("Change label");
        labelButton.addActionListener(new LabelListener());

        JButton colorButton = new JButton("Change cirle");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a labele");
        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().add(BorderLayout.WEST, labelButton);
        frame.getContentPane().add(BorderLayout.EAST, label);
        frame.setSize(300, 300);
        frame.setVisible(true);

        class LabelListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
            label.setText("Ouch!");

            }
        }
        class ColorListener implements ActionListener {
            public void actionPerformed(ActionEvent event) {
            frame.repaint();
            }
        }


        }


    }

I get an error on

labelButton.addActionListener(new LabelListener());

and on

colorButton.addActionListener(new ColorListener());

It says on both occasions that both LabelListener and ColorListener cannot resolved as a type.
Thank you very much in advance..!!

Upvotes: 0

Views: 110

Answers (2)

You need to move the classes LabelListener and ColorListener out of the public void go() Method

class LabelListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        label.setText("Ouch!");
    }
}

and

class ColorListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        frame.repaint();
    }
}

Upvotes: 2

Saketram Durbha
Saketram Durbha

Reputation: 450

In java, you cannot define classes (just like variables, in methods) in methods after you use them, so, instead try defining the classes ColorListener and LabelListener within the class TwoButtons, instead of defining them in the go method, like so: (This is generally better practice)

package twoButtonsPackage;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TwoButtons {
    JFrame frame;
    JLabel label;

    public static void main(String[] args) {
        TwoButtons gui = new TwoButtons();
        gui.go();
    }

    public void go(){
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton labelButton = new JButton("Change label");
        labelButton.addActionListener(new LabelListener());

        JButton colorButton = new JButton("Change cirle");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a labele");
        MyDrawPanel drawPanel = new MyDrawPanel();

        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.getContentPane().add(BorderLayout.WEST, labelButton);
        frame.getContentPane().add(BorderLayout.EAST, label);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    class LabelListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            label.setText("Ouch!");
        }
    }

    class ColorListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            frame.repaint();
        }
    }
}

Upvotes: 1

Related Questions