Java中重写paint()方法后如何添加组件的问题解决办法

public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g; // 强转成2D
		ImageIcon ii1 = new ImageIcon("src/image/pop.png");
		// dimision.width是窗体的宽度,dimision.height是窗体的高度
		g2.drawImage(ii1.getImage(), 0, 0, (bgImage).getIconWidth(), bgImage.getIconHeight(), null);
		
	}
如上图所示,再重写了paint()方法,导致添加组件的时候组件被绘制的图片遮挡住,而当鼠标放上去后又会显示出来,其实解决办法很简单,只需要在paint()方法里再调用
super.paintComponents(g);

方法就可以解决问题了。

public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2 = (Graphics2D) g; // 强转成2D
		ImageIcon ii1 = new ImageIcon("src/image/pop.png");
		// dimision.width是窗体的宽度,dimision.height是窗体的高度
		g2.drawImage(ii1.getImage(), 0, 0, (bgImage).getIconWidth(), bgImage.getIconHeight(), null);
		super.paintComponents(g);
	}


你可能感兴趣的:(面板)