在你的JPanel中重载public void painComponent(Graphics g){……}方法,可在这个方法中直接画图。

解决方案 »

  1.   

    public void paintComponent(Graphics g)
    {
      super.paintComponent(g);
      int imageWidth = this.imageWidth;//根据JPanel的大小来现示图片
      int imageHeight = this.imageHeight;//根据JPanel的大小来现示图片
      
      g.drawImage(image, 0, 0, imageWidth, imageHeight, this);
    }
      

  2.   

    public void paintComponent(Graphics g)
      {
       super.paintComponents(g);
        int imageWidth = logc.getWidth() ;  //根据JPanel的大小来现示图片
        int imageHeight = logc.getHeight();//根据JPanel的大小来现示图片
      Image image=Toolkit.getDefaultToolkit().getImage("pengpeng1.jpg");
        g.drawImage(image, 0, 0, imageWidth, imageHeight, logc);
     logc.doLayout();
      }
    logc是一个jpanel我是在当前的frame即(THIS)下再建了一个logc的jpanel,想在这个jpanel中加入图片,但以上方法仍无效,请高手请教!!多谢!!
      

  3.   

    汗颜...嘿嘿!你的图片要放在当前目录下.还有就是把g.drawImage()中的参数logc改一下(this or null)我也是一个java初学者, : )