我写了一个GUI小程序,在JFrame里设了一张图片作为背景,是在frame的paint()方法里画的,然后加一个Panel作为ContentPane,然后我在Panel设setLayout(null),以后在Panel里加组件就不能将加的组件显示出来了,这是怎么回事,是不是背景图片将加的组件覆盖了,有什么好的办法吗,要求Panel的布局一定是空布局。谢谢,急!!!!

解决方案 »

  1.   

    setLayout(null) 以后 你要把要加在上面的東西 用坐標固定位置 這樣才能知道你放在哪了比如:
          .setBounds(new Rectangle(90,70,200,25));
      

  2.   

    给你看个例子,自己比较一下。
    import java.awt.Graphics;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class TestBackground extends JFrame{
            ImageIcon img;        public TestBackground(){
                    img = new ImageIcon("imgs/background.gif");                JPanel panel = new JPanel(){
                            public void paintComponent(Graphics g){
                                    g.drawImage(img.getImage(), 0, 0, null);
                                    super.paintComponent(g);
                            }
                    };                panel.setOpaque(false);
                    panel.add( new JButton( "Hello" ) );
                    setContentPane( panel );
            }        public static void main(String [] args){
                    TestBackground frame = new TestBackground();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300, 300);
                    frame.setVisible(true);
            }
    }自己该图片路径。
      

  3.   

    我给你写一写我的思路吧!!
    仅是重要的地方的代码。ImageIcon icon = new ImageIcon("你的图片所在的位置");
    JLabel label = JLabel(icon);
    JPanel panel = this.getContentPane();
    panel.add(label);
    label.setBounds(new Rectangle(0,0,100,100));//这里假设图片大小为100*100的
    这些代码的意思就是把你的背景图片放Label上面,然后再加在pannel上面,你可以试试!!
      

  4.   

    JPanel jPanel2 = new JPanel() {    protected void paintComponent(Graphics g) {
            g.drawImage(icon.getImage(), 0, 0, jPanel2.getWidth(),jPanel2.getHeight(),null);
            super.paintComponent(g);
        }
    };
    jPanel2.setOpaque(false);
    将JPanel加到jframe里,这样应该可以
      

  5.   

    我先把Jlable加到到Jpanel中,再把图片加到Jlable中,然后在拖其它的组件,但是就是不现实其它组件呢?那是怎么回事啊?