0

我正在构建一个以图像为背景的 JFrame。我重写了paint() 方法以在JFrame 中绘制该图像,但是当我在Eclipse 中启动应用程序时,我添加的所有JComponent 都不可见。这是我的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);
   }
}
4

3 回答 3

1

不要覆盖 JFrame 的 paint() 方法!!!这不是定制绘画的完成方式。

如果您尝试将背景图像添加到框架中,请查看背景面板以了解几种方法。

于 2013-09-23T17:43:52.863 回答
0

语句按您指定的顺序执行。如果你放置g.drawImage super.paint(g);它之后,它将在绘制其他东西之后绘制图像,即其他东西之上。就像所有的画一样。您稍后绘制的内容将覆盖先前的绘制。

于 2013-09-23T17:32:36.750 回答
0

这是一个关于如何设置 JFrame 背景的好教程。

JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());

或者

setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
于 2013-09-23T18:01:26.850 回答