爪哇超级。paint()在图像上不可见
我正在构建一个以图像为背景的JFrame。我正在重写paint()方法以在JFrame中绘制该图像,但是当我在Eclipse中启动应用程序时,我添加的JComponents都不可见。这是我的SSCCE:
public class foo extends JFrame{
Image i = ImageIO.read(new URL("http://pittsburgh.about.com/library/graphics/regatta_balloons-640.jpg"));
foo(){
setSize(100, 100);
add(new JButton("Foo"));
setVisible(true);
}
@Override public void paint(Graphics g){
super.paint(g);
g.drawImage(i, 0, 0, null);
}
}
# 1 楼答案
不要重写JFrame的paint()方法!!!这不是定制绘画的方式
如果你想在你的相框中添加背景图像,那么请查看Background Panel了解几种方法
# 2 楼答案
语句按您指定的顺序执行。如果你把
g.drawImage
放在super.paint(g);
之后,它会在之后绘制图像,也就是说,在其他东西之上。就像所有的画一样。你以后画的会比以前画的好# 3 楼答案
下面是一个关于如何设置JFrame背景的好方法
或者