我正在做一个画图程序。
我是从 JComponent 派生一个类 DrawCanvas 我希望它在创建好时就用白色来填充背景。
我先这样:
在它的constructor 中加入 Graphics g = this.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());但运行出错,大约是说现在还没有显示,没有这个 Graphics我就在加入 ComponentListener
this.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent e) {
System.out.println("xxxxxxxxx");
Graphics g = this.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
}
});希望DrawCanvas在显示时执行清空背影的代码,
但运行后没有反应。
调试发现根本就没有执行这段代码。我该如何去做?
我是从 JComponent 派生一个类 DrawCanvas 我希望它在创建好时就用白色来填充背景。
我先这样:
在它的constructor 中加入 Graphics g = this.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());但运行出错,大约是说现在还没有显示,没有这个 Graphics我就在加入 ComponentListener
this.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent e) {
System.out.println("xxxxxxxxx");
Graphics g = this.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
}
});希望DrawCanvas在显示时执行清空背影的代码,
但运行后没有反应。
调试发现根本就没有执行这段代码。我该如何去做?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货