2013-12-12 2 views
0

내 프로그램에 음악을 추가하여 상업적 소리처럼 들리려고합니다. Lizza는 PizzaPalooza라는 피자 회사와 관련이 있지만 점점 오전 몇 가지 오류 등 내가 NE를 만들려고 한 이들에 대한 몇 가지 예에 불과,오류 : 클래스 AbstractButton의 addActionListener 메서드를 주어진 유형에 적용 할 수 없습니다.

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

extendActionListener에 의해 무엇을 의미하는지 이해하지의 w 목적? 아니면 다른 방법으로 할 수 있습니까? 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); 
     } 
    } 
} 

답변

2

첫 번째 오류

if(source = playButton) 

이 문제를 던지고 코드의 일부이다

은 할당이며 부울 비교자가 아닙니다. 이 결과는 Button 객체입니다. 대신 다음을 작성해야합니다.

if(source == playButton) 

이중 등가를 유의하십시오.

두 번째 오류

JPizza이하는 ActionListener하지 않습니다 - 당신은 당신이 하나를 사용할 경우이 인터페이스를 확장 할 필요가 : 여러 인터페이스를 구현할 수

public class JPizza extends JFrame implements ActionListener 
+0

예 :' ItemListener, ActionListener' – csvan

+0

을 구현합니다. 이제 'JPizza.java:67'에 대해 오류가 발생합니다. error : 기호를 찾을 수 없습니다. con.add (playButton); ^ 심볼 : 변수 con 위치 : class JPizza' 나는 이것을 JPizza 클래스에서 이동해야합니까? – RequiemDream

+0

당신은'con' :'Container Con = getContentPane();'대신'Con' (대문자 C)를 작성했습니다. 그것을 만드십시오 :'Container con = getContentPane();'그리고 그것은 작동해야합니다. – csvan

관련 문제