RequiemDream
RequiemDream

Reputation: 31

error: method addActionListener in class AbstractButton cannot be applied to given types

I'm trying to add music to my program as to make it sound like a commercial so to say, lol it's in regards to a Pizza company called PizzaPalooza but I am getting a few errors such as

error: method addActionListener in class AbstractButton cannot be applied to given types

JPizza.java:77: error: incompatible types if(source = playButton) ^ required: boolean found: Object

JPizza.java:69: error: method addActionListener in class AbstractButton cannot be applied to given types; playButton.addActionListener(this); ^ required: ActionListener found: JPizza

just a few examples, for these I have tried to create a new object of source

But I do not understand what it means by method addActionListener in class AbstractButton cannot be applied to given types

For these would I have to redo the entire thing and extend the ActionListener? or can I do it another way? I'm having a difficult time understanding the JApplets.

Thanks in Advance!

this is the part of the code that is throwing the problems:

public void init()
{
  Container Con = getContentPane();
  con.setLayout (new FlowLayout());
  con.add(listenLabel);
  con.add(playButton);
  con.add(stopButton);
  playButton.addActionListener(this);
  stopButton.addActionListener(this);
  music = getAudioClip(getCodeBase(),"business.mid");
}

public void actionPerformed(ActionEvent e)
{
  Object source = e.getSource();
  if(source = playButton)
     music.loop();
  else
     music.stop();
}

and this is the rest of my code:

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

public class JPizza extends JFrame implements ItemListener
{
   FlowLayout flow = new FlowLayout();
   JLabel companyName = new JLabel("PizzaPalooza");
   JComboBox<String> sizeBox = new JComboBox<String>();
   JLabel sizeList = new JLabel("Size List");
   JComboBox<String> toppingBox = new JComboBox<String>();
   JLabel toppingList = new JLabel("Topping List");
   JTextField totPrice = new JTextField(10);
   JLabel orderName = new JLabel("Name of Order");
   JTextField oName = new JTextField(15);
   JLabel listenLabel = new JLabel("Listen to our theme!");
   JButton playButton = new JButton("Play");
   JButton stopButton = new JButton("Stop");
   AudioClip music;
   int totalPrice = 0;
   int sizeNum, toppingNum;
   int sPrice, tPrice, sumPrice;
   int[] sizePrice = {0,7,9,11,14};
   int[] toppingPrice = {0,0,1,1,1,1};
   String output;
   public JPizza()
   {
     super("PizzaPalooza");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
     setLayout(flow);
     add(companyName);
     companyName.setFont(new Font("Ariel", Font.BOLD, 20));
     sizeBox.addItem("None");
     sizeBox.addItem("Small");
     sizeBox.addItem("Medium");
     sizeBox.addItem("Large");
     sizeBox.addItem("Extra large");
     toppingBox.addItem("None");
     toppingBox.addItem("Cheese");
     toppingBox.addItem("Sausage");
     toppingBox.addItem("Pepperoni");
     toppingBox.addItem("Green pepper");
     toppingBox.addItem("Onion");
     add(sizeList);
     add(sizeBox);
      sizeBox.addItemListener(this);
     add(toppingList);
  add(toppingBox);
      toppingBox.addItemListener(this);
     add(totPrice);
     add(oName);
     add(orderName);
  }
   public static void main(String[] arguments)
    {
      JPizza pframe = new JPizza();
      pframe.setSize(380,200);
      pframe.setVisible(true);
    }

   public void init()
    {
      Container Con = getContentPane();
      con.setLayout (new FlowLayout());
      con.add(listenLabel);
      con.add(playButton);
      con.add(stopButton);
      playButton.addActionListener(this);
      stopButton.addActionListener(this);
      music = getAudioClip(getCodeBase(),"business.mid");
    }

    public void actionPerformed(ActionEvent e)
    {
      Object source = e.getSource();
      if(source = playButton)
        music.loop();
      else
        music.stop();
    }

  public void itemStateChanged(ItemEvent list)
    {
      Object source = list.getSource();
      if (source == sizeBox)
      {
        sizeNum = sizeBox.getSelectedIndex();
        sPrice = sizePrice[sizeNum];
        sumPrice = sPrice + tPrice;
        output = "Total Price $" + sumPrice;
        totPrice.setText(output);
      }
      else if (source == toppingBox)
       {
         toppingNum = toppingBox.getSelectedIndex();
         tPrice = toppingPrice[toppingNum];
         sumPrice = sPrice + tPrice;
         output = "Total Price $" + sumPrice;
         totPrice.setText(output);
      }
   }
}

Upvotes: 0

Views: 3296

Answers (1)

csvan
csvan

Reputation: 9474

First error

if(source = playButton)

is an assignment, not a boolean comparator. It yields the outcome of the assignment, which in this case is a Button object. Instead, you need to write:

if(source == playButton)

Notice the double-equals.

Second error

JPizza is not an ActionListener - you need to let it extend this interface if you want to use it as one:

public class JPizza extends JFrame implements ActionListener

Upvotes: 2

Related Questions