-1

当我单击其中一个 buffereImage 时,我想发出声音。但它向我显示了这个错误: BufferedImage 类型的方法 addMouseListener(new MouseAdapter(){}) 未定义

这是代码:

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();
    }

}
}
4

1 回答 1

1

MouseListener只能添加到可以在屏幕上显示的组件中。默认情况下, aBufferedImage不能在屏幕上显示,除非先被某些组件包裹(例如JLabel,或绘制到 a 的表面上JPanel)。

实际上,您不能将鼠标侦听器添加到任何不提供支持的东西...

相反,添加MouseListenerDrumorSymbol

您可能需要阅读如何编写鼠标侦听器

于 2013-08-01T03:17:01.940 回答