在"public class MouseEventTest extends Frame implements MouseListener,MouseMotionListener " 这一句中,你只是声明要实现MouseListener,MouseMotionListener 这两个接口并没有说你还要实现WindowListener 这个接口.如果你想这样,就应该加上WindowListener 至于第二个问题,你可以看看有关于MouseListener,MouseMotionListener 这两个接口的定义 这两个接口包含有七个抽象方法, MouseListener中包含五个,分别是: mouseClicked(MouseEvent e); mousePressed(MouseEvent e); mouseReleased(MouseEvent e); mouseEntered(MouseEvent e); mouseExited(MouseEvent e); 而另一个接口MouseMotionListener 只包含两个抽象方法,分别是: mouseDragged(MouseEvent e);和 mouseMoved(MouseEvent e);关于MouseListener,MouseMotionListener 这两个接口的定义的源代码如下: public interface MouseListener extends EventListener { /** * Invoked when the mouse button has been clicked (pressed * and released) on a component. */ public void mouseClicked(MouseEvent e); /** * Invoked when a mouse button has been pressed on a component. */ public void mousePressed(MouseEvent e); /** * Invoked when a mouse button has been released on a component. */ public void mouseReleased(MouseEvent e); /** * Invoked when the mouse enters a component. */ public void mouseEntered(MouseEvent e); /** * Invoked when the mouse exits a component. */ public void mouseExited(MouseEvent e); } public interface MouseMotionListener extends EventListener { /** * Invoked when a mouse button is pressed on a component and then * dragged. <code>MOUSE_DRAGGED</code> events will continue to be * delivered to the component where the drag originated until the * mouse button is released (regardless of whether the mouse position * is within the bounds of the component). * <p> * Due to platform-dependent Drag&Drop implementations, * <code>MOUSE_DRAGGED</code> events may not be delivered during a native * Drag&Drop operation. */ public void mouseDragged(MouseEvent e); /** * Invoked when the mouse cursor has been moved onto a component * but no buttons have been pushed. */ public void mouseMoved(MouseEvent e);}
谢谢大家,我明白问题在哪儿了,可以怎么改正我还是不知道 在public class MouseEventTest extends Frame implements MouseListener,MouseMotionListener 后加了一个,WindowListener编译时又出现以下提示:MouseEventTest.java:5: MouseEventTest is not abstract and does not override abst ract method windowOpened(java.awt.event.WindowEvent) in java.awt.event.WindowLis tener public class MouseEventTest extends Frame implements MouseListener,MouseMotionLi stener,WindowListener不知道怎么回事
那是因为实现的监听鼠标的功能不一样.一个是只在鼠标按键被触动时才触发的事件,一个是只在鼠标移动时才触发的事件,事件类型都不一样,但同样都是监听鼠标,所以都需要MouseEvent类.
2 参见接口的方法原型, implement了这2个接口, 自然需要实现他们声明的方法啦.
这一句中,你只是声明要实现MouseListener,MouseMotionListener 这两个接口并没有说你还要实现WindowListener 这个接口.如果你想这样,就应该加上WindowListener
至于第二个问题,你可以看看有关于MouseListener,MouseMotionListener 这两个接口的定义
这两个接口包含有七个抽象方法,
MouseListener中包含五个,分别是:
mouseClicked(MouseEvent e);
mousePressed(MouseEvent e);
mouseReleased(MouseEvent e);
mouseEntered(MouseEvent e);
mouseExited(MouseEvent e);
而另一个接口MouseMotionListener 只包含两个抽象方法,分别是:
mouseDragged(MouseEvent e);和 mouseMoved(MouseEvent e);关于MouseListener,MouseMotionListener 这两个接口的定义的源代码如下:
public interface MouseListener extends EventListener { /**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*/
public void mouseClicked(MouseEvent e); /**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e); /**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e); /**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e); /**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e);
}
public interface MouseMotionListener extends EventListener { /**
* Invoked when a mouse button is pressed on a component and then
* dragged. <code>MOUSE_DRAGGED</code> events will continue to be
* delivered to the component where the drag originated until the
* mouse button is released (regardless of whether the mouse position
* is within the bounds of the component).
* <p>
* Due to platform-dependent Drag&Drop implementations,
* <code>MOUSE_DRAGGED</code> events may not be delivered during a native
* Drag&Drop operation.
*/
public void mouseDragged(MouseEvent e); /**
* Invoked when the mouse cursor has been moved onto a component
* but no buttons have been pushed.
*/
public void mouseMoved(MouseEvent e);}
2.MouseListener和MouseMotionListener接口方法不同。
在public class MouseEventTest extends Frame implements MouseListener,MouseMotionListener 后加了一个,WindowListener编译时又出现以下提示:MouseEventTest.java:5: MouseEventTest is not abstract and does not override abst
ract method windowOpened(java.awt.event.WindowEvent) in java.awt.event.WindowLis
tener
public class MouseEventTest extends Frame implements MouseListener,MouseMotionLi
stener,WindowListener不知道怎么回事
est.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
又有了下面的出错;
MouseEventTest.java:65: <identifier> expected
test.addWindowListener(new WindowAdapter()
^
郁闷啊我的()没问题啊