你好我是java编程新手,什么时候调用componentResized?你的程序第一次启动时会被调用吗?因为在我的代码中,我没有在屏幕中央添加“rect”,直到窗口调整大小。但每当我启动程序时,“rect”都会添加到屏幕中间。我很抱歉,如果我听起来很模糊,但非常感谢你看看这个。希望你有美好的一天!
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import acm.graphics.GCanvas;
import acm.graphics.GRect;
public class MyCanvas extends GCanvas implements ComponentListener {
public MyCanvas() {
addComponentListener(this);
rect = new GRect(WIDTH, HEIGHT);
rect.setFilled(true);
}
public void update() {
removeAll();
add(rect, (getWidth() - WIDTH) / 2, (getHeight() - HEIGHT) / 2);
}
public void componentResized(ComponentEvent e) {
add(rect, 20, 20);
update();
}
public void componentMoved(ComponentEvent e) {}
public void componentShown(ComponentEvent e) {}
public void componentHidden(ComponentEvent e) {}
private int WIDTH = 30;
private int HEIGHT = 30;
private GRect rect;
}