如何在JFrame添加背景图片。。

解决方案 »

  1.   

    在JFrame上用JLabel添加图片,把JLabel覆盖整个JFrame上就可以了!
    ImageIcon im = new ImageIcon(imagepath);
    JLabel jLabel = new JLabel();
    jLabel.setIcon(im);
    jLabel.setBounds(new Rectangle(x, y, width, height));
    自己改改就可以实现的!
      

  2.   

    老问题了。
    放在JFrame的layeredPane上,然后将content.setOpaque(false)。e.g.:
    package tmp;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class BgFrame extends JFrame {
        public BgFrame() {
            super("Bg Frame Demo");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,300);        ImageIcon img = new ImageIcon("img.jpg");
            JLabel bgLabel = new JLabel(img);
            this.getLayeredPane().add(bgLabel, new Integer(Integer.MIN_VALUE));
            bgLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());        this.getContentPane().add(new JLabel("Hello Background"));
            ((JPanel)getContentPane()).setOpaque(false);        setVisible(true);
        }    public static void main(String[]  args)  {
            BgFrame f = new BgFrame();
        }
      

  3.   

    重写paint方法,在里面画图片
      

  4.   

    JFrame中一般要放诸如JPanel之类的容器,将图片加入JPanel作背景
      

  5.   

    看来很多人连JFrame的结构都分不清楚。
    JFrame首先是有一个JRootPane,这个JRootPane又由两部分组成,glassPane(默认是一个JPanel实例)layeredPane(一个JLayeredPane),而layeredPane再包含我们所熟悉的contentPane和menuBar,我们的组件一般都是放在contentPane上的,因此背景图片最好是放在layeredPane上面。
    要学好Swing不是那么容易的,如果只懂在IDE里拖拉控件的可以说连Swing的门都没有入。
    两种人喜欢在IDE里拖拉Swing控件:
    新手:不懂Swing,只会拖拉,因此会有很多问题搞不掂。
    高手:精通Swing,拖拉控件布局很快,且知道哪些问题不能由拖拉来做。
    --------------------
    呵呵,一时手痒,发发牢骚~~希望多出Swing方面的高人,蔡学庸说他看了几十本Swing方面的英文书,专研了两三年,才敢说自己算是精通了Swing开发,勉之!
      

  6.   

    setComponentPane(new MyPanel());class MyPanel extends JPanel{
    ...public void paintComponent(Graphics g){
    super.paintCoponent(g);Graphics2D g2 = (Graphics2D)g;
    g2.paintImage(...);
    }
    }
      

  7.   

    设置JFrame的ContentPane为一个自定义的JPanel子类,该JPanel类中重写paintComponent(Graphics g)方法,
    方法中绘制一幅图片