本帖最后由 吴亨 于 2011-12-22 20:33 编辑
import java.awt.*;
import java.awt.event.*;
public class MyWorks extends Frame
{
public MyWorks()
{
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
}
}
);
class MyCanvas extends Canvas
{
Image img=null ;
public void paint(Graphics g)
{
if(img == null)
System.out.println("hi");
else
{
g.drawImage(this.img,110,110,this);
}
}
public void setImage(Image img)
{
this.img = img;
}
public MyCanvas()
{
repaint();
}
}
Panel panelPicture = new Panel();
add(panelPicture);
panelPicture.setLayout(new CardLayout());
MyCanvas mc1 = new MyCanvas();
mc1.setImage(mc1.getToolkit().getImage("picture.jpg"));
panelPicture.add(mc1,"1");
}
public static void main(String[] args) {
MyWorks mw = new MyWorks();
}
}
能显示出图片吗?
|