怎么把一个图片添加到窗口体中Java
2个回答
展开全部
package zhidao;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class GameCanvas extends JFrame
{
private static final long serialVersionUID = 1L;
boolean PaintState = true;
static Image soccerImage;
public GameCanvas ()
{
try
{
soccerImage = ImageIO.read (this.getClass ().getResourceAsStream ("/image/luffy.jpg"));
}
catch (IOException e)
{
e.printStackTrace ();
}
}
@Override
public void paint ( Graphics g )
{
g.drawImage (soccerImage, 0, 0, soccerImage.getWidth (null), soccerImage.getHeight (null), this);
}
public static void main ( String[] args )
{
final GameCanvas gc = new GameCanvas ();
gc.setSize (soccerImage.getWidth (null), soccerImage.getHeight (null));
gc.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
gc.addWindowListener (new WindowAdapter()
{
@Override
public void windowIconified ( WindowEvent e )
{
gc.repaint ();
}
});
gc.setVisible (true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询