我想在点击一个buffereImage时发出声音。但它显示了这个错误:方法addMouseListener(new MouseAdapter(){})对于BufferedImage 类型是未定义的
这是代码:
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();
}
}
}
MouseListener
只能添加到可以在屏幕上显示的组件中。默认情况下,BufferedImage
不能在屏幕上显示,除非首先被某些组件包裹(例如JLabel
或绘制在JPanel
的表面上)。
事实上,你不能把鼠标监听器添加到任何不支持它的东西上…
相反,将MouseListener
添加到Drum
或Symbol
类
您可能想阅读一下如何编写鼠标侦听器