-1

I'm doing the double actionlistener exercise detailed in Head First Java, and learn about inner classes, but for some reason my code is not compiling. I'm getting argument invalid errors when I try to call the addActionListener method for JButton.

TwoButtons.java:

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

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 circle");
        colorButton.addActionListener(new ColorListener());

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

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

        frame.setSize(300,300);
        frame.setVisible(true);

    }

    class LabelListener implements ActionListener{
        public void actionPerformed(ActionEvent event){
            label.setText("OUch!");
        }
    }//close inner class

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

}

MyDrawPanel.java: (no errors)

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


public class MyDrawPanel extends JPanel{
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D) g;

        int red = (int)(Math.random()*255);
        int green = (int)(Math.random()*255);
        int blue = (int)(Math.random() * 255);
        Color startColor = new Color(red,green,blue);

        red = (int)(Math.random()*255);
        green = (int)(Math.random()*255);
        blue = (int)(Math.random() * 255);
        Color endColor = new Color(red, green, blue);

        GradientPaint gradient = new GradientPaint(70,70, startColor, 150,150, endColor);
        g2d.setPaint(gradient);
        g2d.fillOval(70, 70, 100, 100);

    }

}
Caleb Jay
  • 2,159
  • 3
  • 32
  • 66

1 Answers1

2

You need to import ActionEvent and ActionListener:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

They reside under java.awt.event.* and not java.awt.*.

If you use an IDE like Eclipse, you can easily import all needed classes with a simple action.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Teach me to use a IDE and automatically use "Organize imports" when testing code from SO. All worked fine. – Jonathan Drapeau Sep 19 '14 at 17:20
  • Ok, so * only includes immediate child imports, rather than just EVERYTHING in awt? Good to know, thank you! I am using eclipse, what's the simple action I need to do? – Caleb Jay Sep 19 '14 at 17:28
  • It's a good practice to explicitly import the individual classes you need (see [this post](http://stackoverflow.com/questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad)). In Eclipse, the shortcut on Windows is Ctrl+Shift+O. – M A Sep 19 '14 at 17:29