使用mouseClicked方法时出错

时间:2013-09-13 11:25:44

标签: java eclipse mouseclick-event

我试图在我的程序中使用mouseClicked类来测试和弄清楚。唯一的问题是每次我使用它时都会收到一个错误,告诉我" void"是mouseClicked的错误类型。我访问的每个网站和教程都向我展示了我应该使用的类型。这是我的代码:

public static void door1(){
    int x = c.getHeight() / 10;
    int y = c.getHeight() * 20 / 100;

    public void mouseClicked(MouseEvent e) {
       c.drawLine("It worked!",y, x);
    }
}

1 个答案:

答案 0 :(得分:0)

您无法在Java中嵌套方法。您的班级必须实施MouseListener才能使用mouseClicked()

您还需要实施:

void mousePressed(MouseEvent e) // Invoked when a mouse button has been pressed on a component.

void mouseReleased(MouseEvent e) // Invoked when a mouse button has been released on a component.

void mouseEntered(MouseEvent e) // Invoked when the mouse enters a component.

void mouseExited(MouseEvent e) // Invoked when the mouse exits a component.