0

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..!!

1
  • 2
    Those are local classes. They are declared inside the method. Declare them before you use them. Commented Apr 9, 2016 at 22:05

2 Answers 2

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.