一段困绕了近半个月的代码:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// System.out.println(image.getWidth()+" "+image.getHeight());
g.drawImage(image,0,0,image.getWidth(),image.getHeight(),null);
setSize(image.getWidth(),image.getHeight());
}。
直到今天才明白错误的原因-----我竟然在paintComponent方法中对面板的大小进行了设置(setSize()),我怎么会傻到如此地步呢,不过说来也搞笑。在paintComponent中调用JPanel的setSize方法,哈哈……,调用setSize方法后,JPanel就又会自动的去调用paintComponent方法,这样就形成的死循环。哈哈……我竟然会犯这种错误。
paintComponent中调用setSize方法
最新推荐文章于 2022-03-10 23:30:41 发布
本文描述了一段导致GUI死循环的代码,问题出现在paintComponent方法中调用了setSize方法,这会导致面板不断重新绘制自身,形成了无限循环。文章揭示了这个常见但容易忽视的错误。
2万+

被折叠的 条评论
为什么被折叠?



