import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;import javax.swing.*;class m 
{ public JPanel jp;
protected ImageIcon icon ;
public int width,height; public m(String photoPath)
{

jp=new JPanel();
icon=new ImageIcon(photoPath);
width=icon.getIconWidth();
height=icon.getIconHeight();
jp.setSize(width,height);
}


JPanel lovd(){
return this.jp;
}

protected void paintComponent(Graphics g)
{
this.jp.paintComponents(g);
Image img=icon.getImage();
g.drawImage(img, 0, 0, this.jp);
}
}public class test
{
private JFrame jf;

test(String Title)
{
jf=new JFrame(Title);

}
void set(){
m lo=new m("d://亲爱的1.jpg");
Dimension ScrSize=Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((ScrSize.width-500)/2);
int y=(int)((ScrSize.height-300)/2);
this.jf.setLayout(null);
this.jf.setBounds(0, 0, ScrSize.width, ScrSize.height);
this.jf.setResizable(false);
this.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.jf.setVisible(true);
this.jf.add(lo.lovd());
}

public static void main(String [] args)
{

new test("test").set();
}


}

解决方案 »

  1.   

    this.jf.setVisible(true);
    this.jf.add(lo.lovd());
    这个反了吧?
      

  2.   

    当然不会显示你的图片了,因为你也没用你m类里的paintComponent方法呀,JPanel里也没加图。感觉你的程序写的有点特别噢,如果你是想
    1 把一张图片放到JPanel上的话,不需要用drawImage呀,paint的,Graphics。。
    2 如果你是想画一张图片放到JPanel上,那就画Graphics,也不需要你的d://亲爱的1.jpg
    仅仅是想把一个image显示在Jpanel上吗?
    很多例子的。
      

  3.   


    jp = new JPanel() {
    public void paintComponent(Graphics g) {
    paintComponents(g);
    Image img = icon.getImage();
    g.drawImage(img, 0, 0, this);
    }
    };另外,m lo = new m("d://1.jpg");改成m lo = new m("d:\\1.jpg");
    原来的那个paintComponent方法是没用的。去掉吧。