我收到错误:
AniWorld is not abstract and does not override abstract method mouseMoved(MouseEvent) in MouseMotionListener
public class AniWorld extends Applet implements MouseMotionListener, MouseListener
代码是:
import java.awt.*;
import java.lang.*;
import java.applet.*;
import java.net.*;
import java.net.MalformedURLException;
import java.applet.Applet.*;
import java.io.IOException;
import java.awt.event.*;
public class AniWorld extends Applet implements MouseMotionListener, MouseListener
{
public Stage stage;
GridBagLayout layout;
public void init()
{
addMouseMotionListener(this);
addMouseListener(this);
setBackground( Color.white );
layout = new GridBagLayout();
setLayout( layout );
int width = Integer.parseInt( getParameter( "width" ).trim() );
int height = Integer.parseInt( getParameter( "height" ).trim() );
stage = new Stage( width-10,height-10);
constrain( stage,this,layout,1,0,1,6,
GridBagConstraints.NONE,
GridBagConstraints.CENTER,0,0 );
stage.addBall();
stage.addRect();
stage.addArc();
stage.start();
}
public void start()
{
stage.start();
} // end of start()
public static void constrain(
Component component,
Container container,
GridBagLayout layout,
int gridx,
int gridy,
int gridwidth,
int gridheight,
int fill,
int anchor,
double weightx,
double weighty
) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.anchor = anchor;
gbc.weightx = weightx;
gbc.weighty = weighty;
layout.setConstraints( component,gbc );
container.add( component );
} // end of constrain()
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
}
答案 0 :(得分:2)
不让GUI类实现侦听器接口的另一个原因。最好的解决方案不是这样做,而是使用扩展MouseAdapter的匿名内部类或执行相同操作的私有内部类。否则,你要求你的GUI做得太多,并在将来为可能的调试问题做好准备。
答案 1 :(得分:1)
您需要添加
public void mouseMoved(MouseEvent)
到AniWorld,或从已实现的接口列表中删除“MouseMotionListener”。
你得到这个错误的原因是因为当你说你实现一个接口时,你必须为接口中的所有方法提供实现,或者你的类需要是抽象的。
答案 2 :(得分:1)
您必须实施(或至少留下空白实施)方法MouseDragged(MouseEvent e)
和MouseMoved(MouseEvent e)
,因为您正在实施MouseMotionListener
界面。
答案 3 :(得分:0)
是AniWorld, 你必须覆盖接口的所有方法,意味着如果你正在实现MouseListener,那么你必须覆盖所有五个方法,如果你要留下一个方法来覆盖那么100%你会得到同样的错误......你正在实现两个监听器,所以你必须覆盖,mouseCliked(),mouseEntered(),mouseExited(),mousePressed(),mouseReleased(),mouseDragged(),mouseMoved()
在所有方法覆盖后,您将成功,没有任何错误....