import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class WindowFrame extends JFrame
{
    Image myIM;
    public void setImage(Image im)
    {
     this.myIM=im;
     System.out.println("SetP");
    }
    
    public WindowFrame()
    {
        JPanel panel = new JPanel()
        {
            protected void paintComponent(Graphics g)
            {
                g.drawImage(myIM, 0, 0, null);
                super.paintComponent(g);
            }
        };
        panel.setOpaque( false );
        JButton button = new JButton( "Hello" );
        panel.add( button );
        System.out.println("Panel");
        //initGUI();
    }
 
    public static void initGUI()
    {
     WindowFrame frame = new WindowFrame();
        frame.setSize(300, 300);
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
        System.out.println("GUI");
    }
    
    public static void main(String [] args)
    {
     initGUI();
        System.out.println("Main");
    }
}我在别的类先实体化:WindowFrame mywf=new WindowFrame()里面调用这个类的myfm.setImage(im)来传入一个图片.按说调用mywf.repaint();就应该画图的啊...可是就是不画,连框都不显示!!救命啊..后天就要交作业了...5555


解决方案 »

  1.   

    因为你没有把panel添加到frame里面,在你的 public WindowFrame()方法里面加一句
    add(panel);就好了
      

  2.   

    谢谢了,但不是这样的.这个类可以单独的运行的!只要定义下图片的路径,单独运行是没问题的.但是一但是用别的类调用了并传了image,他就不画了..其实我在里面各个方法中加了system.out.println做监测,发现,一旦是别的类调用他的话,他的main就不执行了..
      

  3.   

    1.代码实在是写得~~
    2.概念问题 你把initGUI搞成静态?WindowFrame构造函数里面,事例一个对象才执行一次.
      实在是没法讲~~应该多看看书
    3.JFRAME里面是装不了JFRAME的~~哎 LZ应该多看点书
    ===>帮你代码改好了 单执行或直接加在别的JFRAME里都行  快点结了 偶还茶50分 两星了 嘿嘿
    package csdn;import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.imageio.ImageIO;
    import java.io.*;public class WindowFrame extends JPanel {
        Image myIM;
        JPanel panel;
        public void setImage(Image im) {
            this.myIM = im;
            System.out.println("SetP");
        }    public WindowFrame() {
        }
        public void initGUI() {
            panel = new JPanel() {
                protected void paintComponent(Graphics g) {
                    g.drawImage(myIM, 0, 0, null);
                    super.paintComponent(g);
                }
            };
            panel.setOpaque(false);
            JButton button = new JButton("Hello");
            panel.add(button);
            System.out.println("Panel");
            this.add(panel);
            //initGUI();
           System.out.println("GUI");
        }    public static void main(String[] args) {
            JFrame jframeComtainer=new JFrame();
            WindowFrame frame = new WindowFrame();
            Image image = null;
            try {
                image = ImageIO.read(new File("c:/text.png"));
            } catch (IOException ex) {
                System.out.println("ex=" + ex);
            }
            frame.setImage(image);
            frame.initGUI();
            jframeComtainer.add(frame);
            jframeComtainer.setLocationRelativeTo(null);
            jframeComtainer.setVisible(true);
            jframeComtainer.setSize(300, 300);
            //   initGUI();
            System.out.println("Main");
        }
    }
      

  4.   

    把 g.drawImage(myIM, 0, 0, null); 换成 g.drawImage(myIM, 0, 0, this); 就可以了