Я пытаюсь добавить музыку в свою программу, чтобы она звучала как коммерческая, так сказать, LOL в отношении компании Pizza под названием PizzaPalooza, но я Я получаю несколько ошибок, таких какerror: метод addActionListener в классе AbstractButton не может быть применен к заданным типам
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);
}
}
}
да, вы можете реализовать несколько интерфейсов: ' реализует ItemListener, ActionListener' – csvan
теперь он бросает ошибки для 'JPizza.java:67: ошибка: не может найти символ con.add (playButton); ^ символ: переменная con местоположение: класс JPizza' мне пришлось бы переместить это из класса JPizza? – RequiemDream
Вы писали 'Con' (капитал C) вместо' con': 'Container Con = getContentPane();'. Сделайте это: 'Container con = getContentPane();' и он должен работать. – csvan