gpt4 book ai didi

java - 错误 MouseMotionListener、MouseListener

转载 作者:行者123 更新时间:2023-12-01 19:11:41 26 4
gpt4 key购买 nike

我收到错误:

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) {}
}

最佳答案

不让 GUI 类实现监听器接口(interface)的另一个原因。最好的解决方案不是这样做,而是使用扩展 MouseAdapter 的匿名内部类或执行相同操作的私有(private)内部类。否则,您会要求 GUI 做太多事情,并且会为将来可能出现的调试问题做好准备。

关于java - 错误 MouseMotionListener、MouseListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com