当前位置: 代码迷 >> J2SE >> 在jpanel上展示本地磁盘上的图象
  详细解决方案

在jpanel上展示本地磁盘上的图象

热度:240   发布时间:2016-04-24 18:01:11.0
在jpanel上显示本地磁盘上的图象?
请问如何在Jpanel上用Graphics.drawImage()显示本地磁盘上的图象?谢谢

------解决方案--------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends Frame
{

public Test()
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
});
}

public static void main(String args[])
{
Test mainFrame = new Test();
JPanel jpanel=new JPanel();
Image img = mainFrame.getToolkit().getImage( "c:\\bag.png ");
mainFrame.add(jpanel);
mainFrame.setSize(400, 400);
mainFrame.setTitle( "DrawImage ");
mainFrame.setVisible(true);
Graphics g = jpanel.getGraphics();
while (!g.drawImage(img, 100, 100, mainFrame));
System.out.println( "Start Drawing! ");
}
}