我有一个关于java的快速问题。为什么这样做有效:
list.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("Mouse click.");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
});
但这不是:
list.addMouseListener(new MouseAdapter(){
public void mouseClick(MouseEvent e) {
System.out.println("Mouse click.");
}
});
感谢您提前回答。
PS我不得不添加更多文字,因为烦人的后添加脚本不会让我,因为我的帖子主要是代码。
答案 0 :(得分:7)
mouseClicked而非
mouseClick小拼写错误?
答案 1 :(得分:0)
抽象类MouseAdapter实现了MouseListener接口以及:
public void **mouseClicked**(MouseEvent e) {}
而不是:
public void **mouseClick**(MouseEvent e)
这种方式应该有效:
list.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse click.");
}
});