I keep getting an error with the "ButtonListener." I don't know what it is that's causing it, but help would be appreciated.

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MovingCarPanel extends JPanel
{
  private int delay = 2;
  private Car car;
  private Timer timer;
  private JButton accelerate, decelerate;
  private JPanel buttonPanel;

  int x, y;
  int moveX;

  public MovingCarPanel()
  {
    accelerate = new JButton ("Accelerate");
    decelerate = new JButton ("Decelerate");

    ButtonListener listener = new ButtonListener();
    accelerate.addActionListener(listener);
    decelerate.addActionListener(listener);

    buttonPanel = new JPanel();
    buttonPanel.setPreferredSize(new Dimension(200, 40));
    //buttonPanel.setBackground (Color.blue);
    buttonPanel.add (accelerate);
    buttonPanel.add (decelerate);

    add (buttonPanel);

    timer = new Timer(delay, null);
    timer.addActionListener(new SideCarListener());
    car = new Car(50, 90, 90, 15);

    setBackground(Color.white);
    setPreferredSize(new Dimension(700, 500));
    addMouseListener(new MouseCarListener());

    x = 50;
    y = 300;
    moveX = 1;
    timer.start();
  }

  public void paintComponent (Graphics page)
  {
    super.paintComponent(page);
    car.draw(page);
  }

  private class MouseCarListener implements MouseListener
  {
    public void mousePressed(MouseEvent event)
    {
        moveX = -1*moveX;
    }
    public void mouseClicked(MouseEvent event){}
    public void mouseReleased(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
  }

  private class SideCarListener implements ActionListener
  {
    public void actionPerformed(ActionEvent event)
    {
      if(x == 700 && moveX > 0)
        x = -90;
      else if(x < -90 && moveX < 0)
        x = 700;

      car.setX(x);
      car.setY(y);

      repaint();
    }
  }
}

Thank you!

I keep getting an error

Please post the full text of the error message.

2 errors found:
[line: 24]
Error: cannot find symbol
symbol: class ButtonListener
location: class MovingCarPanel
[line: 24]
Error: cannot find symbol
symbol: class ButtonListener
location: class MovingCarPanel

Where is the class: ButtonListener defined? The compiler can not find its definition.

perhaps you are using the wrong name? because i cant find a class called ButtonListener?

I thought ButtonListener was in the JButton class?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.