我正在尝试将音乐添加到我的程序中,以使其听起来像商业广告,所以说,就像一家名为PizzaPalooza的披萨公司而言,我会收到一些错误,例如
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
仅举几个例子,为此我尝试创建source
但我不明白method addActionListener in class AbstractButton cannot be applied to given types
对于这些,我必须重做整个事情并extend
ActionListener
吗?或者我可以用另一种方式吗?我很难理解JApplets
。
提前致谢!
这是抛出问题的代码的一部分:
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();
}
这是我的其余代码:
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);
}
}
}
答案 0 :(得分:2)
第一个错误
if(source = playButton)
是赋值,而不是布尔比较器。它产生赋值的结果,在本例中是Button对象。相反,你需要写:
if(source == playButton)
注意双等号。
第二次错误
JPizza不是ActionListener - 如果要将其用作一个接口,则需要让它扩展此接口:
public class JPizza extends JFrame implements ActionListener