public class Frame1 extends JFrame implements ActionListener {
 JPanel jPanel3 = new JPanel(){
       //绘制背景
        public void paintComponent(Graphics g) {        if(image!=null){            g.drawImage(image,0,0,this.getWidth(),this.getHeight(),null);
        }else{
            g.setColor(new Color(100, 155, 100));
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(new Color(80, 135, 80));
            g.setFont(new Font("黑体", 1, 25));
            g.drawString("无背景图片", this.getWidth()/2, 30);
        }
        }    };
。 /**
   *加载背景图
   * @return boolean
   */
  public boolean loadBgPic(){
      imageIcon=new ImageIcon(this.bgPicname);
      image=imageIcon.getImage();
      image = image.getScaledInstance(jPanel3.getWidth(), jPanel3.getHeight(), Image.SCALE_SMOOTH);     jPanel3.validate();
     jPanel3.repaint();      return true;
  }

}
上面的程序中,我在jPanel3里加了一幅背景图片,然后,在jFram1里定义loadBgPic用来生成image
现在的问题是,调用后,jPanel3显示的是jframe1的背景色,这时如果jPanel3被遮挡重绘时,就会显示部分背景图片,但为何第一次 jPanel3.validate();
     jPanel3.repaint();不显示呢?

解决方案 »

  1.   

    有没有调试啊?
    关键是你的loadBgPic是什么时候调用的恶,如果在jpanel3初始化之前调用,那肯定是可以的,如果在jpanel3初始化后调用,第一次肯定是不会出来的,而是默认的颜色,类似于灰色。
    建议把源码都贴出来看看
      

  2.   

    在paintComponent的第一行加上super.paintComponent(g);
      

  3.   

    加不加super.paintComponent(g);
    结果都一样to diggywang(Miner Lover!) 
    loadBgPic是我点击按钮时候,选择背景图片文件,然后调用
    所以肯定是jpanel3初始化以后
    但是,现在我调用后,jPanel3.validate();
         jPanel3.repaint();
    我也看到输出了,的确调用了jPanel3的paintComponent方法
    但问题是,随后,jPanel3有被frame1的背景色覆盖了
    如果拖动程序,让jPanel3部分被窗口遮挡,然后再移动,系统会调用paintComponent将被遮挡部分绘制上我所设置的图片。
      

  4.   

    你把JPanel的背景涂成红色,看看只是图片没出来,还是整个JPanel没出来
      

  5.   

    package gktsys.frame;import java.awt.BorderLayout;import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Dimension;
    import java.awt.event.MouseEvent;
    import java.awt.event.*;
    import javax.swing.BorderFactory;
    import java.awt.FlowLayout;
    import java.awt.Color;
    import java.awt.SystemColor;
    import javax.swing.JLabel;
    import java.awt.Point;
    import java.io.*;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.FileDialog;
    import java.awt.Image;
    import java.awt.Graphics;
    import javax.swing.ImageIcon;
    import java.awt.Font;public class Frame1 extends JFrame implements ActionListener {
        //背景图片
          private ImageIcon imageIcon = null;
         transient   Image image = null;
        JPanel jPanel1 = new JPanel();
        JButton jButton2 = new JButton();
        FlowLayout flowLayout2 = new FlowLayout();
        JPanel jPanel2 = new JPanel();
        //主编辑panel
        JPanel jPanel3 = new JPanel(){
           //绘制背景
            public void paintComponent(Graphics g) {        if(image!=null){
                g.drawImage(image,0,0,this.getWidth(),this.getHeight(),null);
            }else{
                g.setColor(new Color(100, 155, 100));
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                g.setColor(new Color(80, 135, 80));
                g.setFont(new Font("黑体", 1, 25));
                g.drawString("无背景图片", this.getWidth()/2, 30);
            }
            }
        };
        int x=0;
        int y=0;
        JButton jButton3 = new JButton();
        int START=50;
        BorderLayout borderLayout1 = new BorderLayout();
        //重载
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource().getClass().getName().equals("javax.swing.JButton"))//getSource判断哪个按钮被按下了。
            {
               //用来选择图片的
               FileDialog filedlg=new FileDialog(this,"选择背景图片");           filedlg.setVisible(true);
               String picpath=filedlg.getDirectory();
               String  picfile=filedlg.getFile();
               System.out.println(picpath+picfile);
               imageIcon=new ImageIcon(picpath+picfile);
               image=imageIcon.getImage();
               image = image.getScaledInstance(jPanel3.getWidth(), jPanel3.getHeight(), Image.SCALE_SMOOTH);
               jPanel3.validate();
               jPanel3.updateUI();
                    
            }
        }    public Frame1() {
            try {
                jbInit();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        private void jbInit() throws Exception {
            getContentPane().setLayout(borderLayout1);
            jButton2.setText("添加标签");
            jButton2.addMouseListener(new Frame1_jButton2_mouseAdapter(this));
            jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
            jPanel1.setBorder(null);
            jPanel1.setMaximumSize(new Dimension(7670, 7670));
            jPanel1.setLayout(flowLayout2);
            this.getContentPane().setBackground(SystemColor.controlText);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("test");
            this.setVisible(true);
            jPanel2.setBorder(null);
            jPanel2.setPreferredSize(new Dimension(100, 10));
            jPanel3.setBounds(0,0,800,600);
            jPanel3.setBackground(SystemColor.activeCaptionBorder);
            jPanel3.setBorder(BorderFactory.createLoweredBevelBorder());
            jPanel3.setOpaque(false);
            jPanel3.setInputVerifier(null);
            jPanel3.setLayout(null);
            jPanel3.addMouseListener(new Frame1_jPanel3_mouseAdapter(this));
            jPanel3.addMouseMotionListener(new Frame1_jPanel3_mouseMotionAdapter(this));
            jButton3.setText("加载图像");
            jButton3.addActionListener(this);
            jPanel1.add(jButton3);
            jPanel1.add(jButton2, null);
            this.getContentPane().add(jPanel2, java.awt.BorderLayout.EAST);
            this.getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);
            this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
        }    public static void main(String[] args) {
            Frame1 frame1 = new Frame1();
            frame1.setSize(800,600);
            frame1.setVisible(true);
        }    public void jButton2_actionPerformed(ActionEvent e) {    }//增加测点
        public void jButton2_mouseClicked(MouseEvent e) {        System.out.println("clicked");
            Point p=new Point(50+START,100);
             JLabel jcedian = new JLabel();
                 jcedian.setText("xin cedian");
                 jcedian.setBounds(50+START,100,48,15);
                  jPanel3.add(jcedian);
                   START+=50;
                 jPanel3.validate();
                  jPanel3.repaint();
        }
    }
    class Frame1_jPanel3_mouseAdapter extends MouseAdapter {
        private Frame1 adaptee;
        Frame1_jPanel3_mouseAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }
    }class Frame1_jPanel3_mouseMotionAdapter extends MouseMotionAdapter {
        private Frame1 adaptee;
        Frame1_jPanel3_mouseMotionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }
    }class Frame1_jButton2_actionAdapter implements ActionListener {
        private Frame1 adaptee;
        Frame1_jButton2_actionAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void actionPerformed(ActionEvent e) {
            adaptee.jButton2_actionPerformed(e);
        }
    }
    class Frame1_jButton2_mouseAdapter extends MouseAdapter {
        private Frame1 adaptee;
        Frame1_jButton2_mouseAdapter(Frame1 adaptee) {
            this.adaptee = adaptee;
        }    public void mouseClicked(MouseEvent e) {
            adaptee.jButton2_mouseClicked(e);
        }
    }
    这是源程序,运行后,加载一jpg格式的图片,会出现黑色背景将jPanel3覆盖
      

  6.   

    黑色背景,会不会是imageBuffer没装载图片阿。
      

  7.   

    哦  没用imageBuffer啊,反正用到哪个放的图片,调试一下看图片放进去没,否则应该不会出黑色背景的。
      

  8.   

    单步时候,jPanel3.paintComponent被执行了
      

  9.   

    等等g.drawImage(image,0,0,this.getWidth(),this.getHeight(),null);
    改为
    g.drawImage(image,0,0,this.getWidth(),this.getHeight(),this);ImageObserver怎么设置成null了?要用绘制面板才可以
      

  10.   

    来晚一步啊,重载的方法drawImage()传null参数,哎,太不小心了,这些个情况下传null是最容易出错的
      

  11.   

    package gktsys.frame;import java.awt.BorderLayout;import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Dimension;
    import java.awt.event.MouseEvent;
    import java.awt.event.*;
    import javax.swing.BorderFactory;
    import java.awt.FlowLayout;
    import java.awt.Color;
    import java.awt.SystemColor;
    import javax.swing.JLabel;
    import java.awt.Point;
    import java.io.*;
    import java.awt.Component;
    import java.awt.Frame;
    import java.awt.FileDialog;
    import java.awt.Image;
    import java.awt.Graphics;
    import javax.swing.ImageIcon;
    import java.awt.Font;
    import java.awt.*;public class Frame1
        extends JFrame
        implements ActionListener {
      //背景图片
      private ImageIcon imageIcon = null;
      transient Image image = null;
      JPanel jPanel1 = new JPanel();
      JButton jButton2 = new JButton();
      FlowLayout flowLayout2 = new FlowLayout();
      JPanel jPanel2 = new JPanel();
      //主编辑panel
      JPanel jPanel3 = new JPanel() {
        //绘制背景
        public void paintComponent(Graphics g) {
          super.paintComponent(g);
          g.setColor(new Color(100, 155, 100));
          g.fillRect(0, 0, this.getWidth(), this.getHeight());
          if (image != null) {
            g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
          }
          else {
            g.setColor(new Color(80, 135, 80));
            g.setFont(new Font("黑体", 1, 25));
            g.drawString("无背景图片", this.getWidth() / 2, 30);
          }
        }
      };
      int x = 0;
      int y = 0;
      JButton jButton3 = new JButton();
      int START = 50;
      BorderLayout borderLayout1 = new BorderLayout();
      //重载
      public void actionPerformed(ActionEvent e) {
        if (e.getSource().getClass().getName().equals("javax.swing.JButton")) { //getSource判断哪个按钮被按下了。
          //用来选择图片的
          FileDialog filedlg = new FileDialog(this, "选择背景图片");      filedlg.setVisible(true);
          String picpath = filedlg.getDirectory();
          String picfile = filedlg.getFile();
          System.out.println(picpath + picfile);
          imageIcon = new ImageIcon(picpath + picfile);
          image = imageIcon.getImage();
          image = image.getScaledInstance(jPanel3.getWidth(), jPanel3.getHeight(),
                                          Image.SCALE_SMOOTH);
          MediaTracker mt=new MediaTracker(this);
          mt.addImage(image,0);
          try {
            mt.waitForID(0);
          }
          catch (InterruptedException ex) {
          }
          jPanel3.validate();
          jPanel3.updateUI();    }
      }  public Frame1() {
        try {
          jbInit();
        }
        catch (Exception exception) {
          exception.printStackTrace();
        }
      }  private void jbInit() throws Exception {
        getContentPane().setLayout(borderLayout1);
        jButton2.setText("添加标签");
        jButton2.addMouseListener(new Frame1_jButton2_mouseAdapter(this));
        jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));
        jPanel1.setBorder(null);
        jPanel1.setMaximumSize(new Dimension(7670, 7670));
        jPanel1.setLayout(flowLayout2);
        this.getContentPane().setBackground(SystemColor.controlText);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("test");
        this.setVisible(true);
        jPanel2.setBorder(null);
        jPanel2.setPreferredSize(new Dimension(100, 10));
        jPanel3.setBounds(0, 0, 800, 600);
        jPanel3.setBackground(SystemColor.activeCaptionBorder);
        jPanel3.setBorder(BorderFactory.createLoweredBevelBorder());
        jPanel3.setOpaque(false);
        jPanel3.setInputVerifier(null);
        jPanel3.setLayout(null);
        jPanel3.addMouseListener(new Frame1_jPanel3_mouseAdapter(this));
        jPanel3.addMouseMotionListener(new Frame1_jPanel3_mouseMotionAdapter(this));
        jButton3.setText("加载图像");
        jButton3.addActionListener(this);
        jPanel1.add(jButton3);
        jPanel1.add(jButton2, null);
        this.getContentPane().add(jPanel2, java.awt.BorderLayout.EAST);
        this.getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);
        this.getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);
      }  public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
        frame1.setSize(800, 600);
        frame1.setVisible(true);
      }  public void jButton2_actionPerformed(ActionEvent e) {  }//增加测点
      public void jButton2_mouseClicked(MouseEvent e) {    System.out.println("clicked");
        Point p = new Point(50 + START, 100);
        JLabel jcedian = new JLabel();
        jcedian.setText("xin cedian");
        jcedian.setBounds(50 + START, 100, 48, 15);
        jPanel3.add(jcedian);
        START += 50;
        jPanel3.validate();
        jPanel3.repaint();
      }
    }class Frame1_jPanel3_mouseAdapter
        extends MouseAdapter {
      private Frame1 adaptee;
      Frame1_jPanel3_mouseAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
      }
    }class Frame1_jPanel3_mouseMotionAdapter
        extends MouseMotionAdapter {
      private Frame1 adaptee;
      Frame1_jPanel3_mouseMotionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
      }
    }class Frame1_jButton2_actionAdapter
        implements ActionListener {
      private Frame1 adaptee;
      Frame1_jButton2_actionAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
      }  public void actionPerformed(ActionEvent e) {
        adaptee.jButton2_actionPerformed(e);
      }
    }class Frame1_jButton2_mouseAdapter
        extends MouseAdapter {
      private Frame1 adaptee;
      Frame1_jButton2_mouseAdapter(Frame1 adaptee) {
        this.adaptee = adaptee;
      }  public void mouseClicked(MouseEvent e) {
        adaptee.jButton2_mouseClicked(e);
      }
    }好了我修改了两个地方
    一个是paintComponent方法,这个不碍事,不是原因所在,不过最好注意一下
    还有一个是Image加载的地方,要用MediaTracker来等待,等到Image加载完毕为止。应为new Image()虽然方法调用完毕,但图片加载还没完毕。
    建议加载图片的方法放到Thread里面写哈哈,那个帖子没看到哦
      

  12.   

    to sxnucseven(*) 
    什么人?一句玩笑而已,他给分我没看到那个帖子而已。你有能耐去解决问题呀。
      

  13.   

    http://community.csdn.net/Expert/topic/5223/5223973.xml?temp=.6551172赫赫,多谢了