求救!java2参考大全里的了一个例子,我看了100次程序没错,就是达不到遇期的效果!
程序是这样的:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ResizeMe extends Applet{
final int inc=25;
int max=500;
int min=200;
Dimension d;
public ResizeMe()
{
addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
int w=(d.width+inc)> max?min:(d.width+inc);
int h=(d.height+inc)> max?min:(d.height+inc);
}
});
}
public void paint(Graphics g)
{
d=getSize();
g.drawLine(0,0,d.width-1,d.height-1);
g.drawLine(0,d.height-1,d.width-1,0);
g.drawLine(0,0,d.width-1,d.height-1);
}
}
程序是书上的,我照住打,程序运行没出错,只是不能监听到mouseReleased这个事件!我在哪里错了?高手指点!
------解决方案--------------------你在mouseReleased里面就定义和改变了2个整数,视觉上是看不到是否监听到了该事件,
你在mouseReleased事件里加2行再看看:
addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e){
int w=(d.width+inc)> max?min:(d.width+inc);
int h=(d.height+inc)> max?min:(d.height+inc);
getGraphics().drawString(Integer.toString(w), 5, 15);
getGraphics().drawString(Integer.toString(h), 5, 25);
}
});