我正在尝试学习小程序,并且在执行此代码时,没有编译时错误,但问题是我对我的代码没有任何响应。 我使用简单的记事本和小程序查看器来完成它。单击小程序时,应该显示坐标,但没有发生。我尝试输入相关的html代码并通过浏览器执行,但响应是相同的。任何帮助将不胜感激
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MyMouseApplet.class"
width = "400"
height = "400">
</applet>*/
public class MyMouseApplet extends Applet implements MouseListener{
int x,y;
String str=" ";
public void init(){
this.addMouseListener(this);
}
public void paint(Graphics g){
g.drawString(str,x,y);
}
public void update(Graphics g){
paint(g);
}
public void mouseClicked(MouseEvent m)
{
int x = m.getX();
int y = m.getY();
str="x:" + x + "y:" + y;
repaint();
}
public void mouseExited(MouseEvent m){}
public void mouseEntered(MouseEvent m){}
public void mousePressed(MouseEvent m){}
public void mouseReleased(MouseEvent m){}
}
您的mouseClicked
函数将x
和y
超出范围。这样做:
public void mouseClicked(MouseEvent m)
{
//int x = m.getX();
//int y = m.getY();
x = m.getX();
y = m.getY();
str="x:" + x + "y:" + y;
repaint();
}
这将访问类的字段,而不是局部变量。