Jay
Jay

Reputation: 109

Event handling done by an anonymous inner class

The program consists of simply adding buttons to a panel and when the button is clicked the screen should turn into the color of the specified button. Basically I need to change the blue button do the exact same thing, but with an anonymous inner class and I feel like I am sort of on the right track, but I am getting a long list of errors. I have looked at many examples of anonymous inner classes and I believe I am writing the code correctly, but there are a lot of errors having to do with the compiler not being able to find the symbols, which I do not fully understand. Any help will be appreciated because I have been working on this for 2 days now and I am hoping to fix it by this week. Here is my code: (A lot of things are commented out so I can focus on one button at a time)

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

this is where I am trying to add the anonymous inner class:

class Blue
{
  public void start()
  {
     ActionListener listener = new Blue();
  }
  public class Blue implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
            Object source = evt.getSource();
            Color color = getBackground();
            color = Color.blue;
            setBackground(color);
            repaint();
    }
  }
}

class ButtonPanel extends JPanel //implements ActionListener
{
   private JButton yellowButton;
   private JButton blueButton;
   private JButton redButton;
   private JButton greenButton;

public ButtonPanel()
{
    //yellowButton = new JButton("Yellow");
    //redButton = new JButton("Red");
    blueButton = new JButton("Blue");
    //greenButton = new JButton("Green");

    //add(yellowButton);
    //add(redButton);
    add(blueButton);
    //add(greenButton);

    //yellowButton.addActionListener(this);

    blueButton.addActionListener(listener);
    //greenButton.addActionListener(this);

    /*class Red
    {
        public void red()
        {
            ActionListener listener = new ActionListener();
            redButton.addActionListener(listener);
        }
        class turnRed implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                setBackground(Color.red);
                repaint();
            }
        }
    }*/
}


/*public void actionPerformed(ActionEvent evt)
{
    Object source = evt.getSource();
    Color color = getBackground();
    if (source == yellowButton) color = Color.yellow;
    else if (source == blueButton) color = Color.blue;
    else if (source == redButton) color = Color.red;
    else if (source == greenButton) color = Color.green;
    setBackground(color);
    repaint();
}
}


class ButtonFrame extends JFrame
{
  public ButtonFrame()
  {
    setTitle("ButtonTest");
    setSize(300, 200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new ButtonPanel());
  }
}

public class ButtonTest
{
   public static void main(String[] args)
   {
      JFrame frame = new ButtonFrame();
      frame.setVisible(true);
   }
}

Upvotes: 0

Views: 3797

Answers (1)

bruno
bruno

Reputation: 2273

Instead of using the listenervariable, create and instance of a (newly created) anonymous inner class. This is how it's done:

blueButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        Object source = evt.getSource();
        Color color = getBackground();
        color = Color.blue;
        setBackground(color);
        repaint();
    }
});

Upvotes: 1

Related Questions