gpt4 book ai didi

java - 错误: method addActionListener in class AbstractButton cannot be applied to given types

转载 作者:行者123 更新时间:2023-12-02 06:25:55 24 4
gpt4 key购买 nike

我正在尝试向我的程序添加音乐,使其听起来像商业广告,哈哈,这是关于一家名为 PizzaPalooza 的披萨公司,但我遇到了一些错误,例如

错误:AbstractButton类中的方法addActionListener无法应用于给定类型

JPizza.java:77:错误:类型不兼容
if(源=播放按钮)
^
必需: boolean 值
发现:对象

JPizza.java:69: 错误:AbstractButton 类中的方法 addActionListener 无法应用于给定类型;
playButton.addActionListener(this);
^
必需: Action 监听器
发现:JPizza

只是几个例子,对于这些我尝试创建一个新的source对象

但我不明白AbstractButton类中的方法addActionListener不能应用于给定类型是什么意思

对于这些,我是否必须重做整个事情并扩展 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);
}
}
}

最佳答案

第一个错误

if(source = playButton)

是一个赋值,而不是一个 boolean 比较器。它产生赋值的结果,在本例中是一个 Button 对象。相反,您需要编写:

if(source == playButton)

注意双等号。

第二个错误

JPizza 不是一个 ActionListener - 如果您想将其用作一个 ActionListener,则需要让它扩展此接口(interface):

public class JPizza extends JFrame implements ActionListener

关于java - 错误: method addActionListener in class AbstractButton cannot be applied to given types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20534337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com