2013-08-01 27 views
-1

我想在點擊我的某個buffereImage時發出聲音。但它顯示我這個錯誤:該方法addMouseListener將(新MouseAdapter(){})是未定義的類型的BufferedImage向BufferedImage添加mouseListener時出錯

這裏的代碼:

public class TestPane extends JPanel { 

    public TestPane() { 
     setLayout(new PropertionalLayoutManager(400, 400)); 
     add(new Symbol(), new PropertionalConstraint(0f, 0)); 
     add(new Symbol(), new PropertionalConstraint(0.67f, 0)); 
     //add(new Symbol(), new PropertionalConstraint(0f, 0.4675f)); 
     //add(new Symbol(), new PropertionalConstraint(0.67f, 0.4675f)); 
     add(new Drum(), new PropertionalConstraint(0.205f, 0.1f)); 
     add(new Drum(), new PropertionalConstraint(0.5f, 0.1f)); 
     add(new Drum(), new PropertionalConstraint(0f, 0.33f)); 
     add(new Drum(), new PropertionalConstraint(0.705f, 0.33f)); 


    DRUM.addMouseListener(new MouseAdapter() 
    { 
      public void mouseClicked(MouseEvent me) 
      { 
      Sound1.Sound5.play(); 
      } 
     }); 


    }static { 

    try { 
     SYMBOL = ImageIO.read(new File("HiCrash.png")); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
    try { 
     DRUM = ImageIO.read(new File("HiTom.png")); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

} 
} 
+1

'MouseListener'只能添加到可以在屏幕上顯示的組件。默認情況下,一個'BufferedImage'不能在屏幕上顯示,沒有先被某個組件包裝(比如'JLabel'或者繪製到'JPanel'的表面上)。 – MadProgrammer

+0

那麼如何將bufferedImage插入JPanel? – jat

+0

看看我的回答;) – MadProgrammer

回答

1

MouseListener只能被添加到一個組件,可以在屏幕上可見。默認情況下,BufferedImage不能在屏幕上顯示,沒有先被某個組件包裹(例如JLabel或塗在JPanel的表面上)。

其實,你不能鼠標監聽器添加到任何不提供對它的支持...

相反,MouseListener添加到DrumSymbol

您可能要通過讀取How to write a mouse listener

+0

謝謝@MadProgrammer。我還是不太瞭解。你幫我很多 – jat

+0

我似乎學得越多,我似乎就知道的越少:P。最好的老師是經驗,不斷嘗試,看看它需要你 – MadProgrammer