您的位置:首页 > 其它

图形界面---显示图像

2016-03-27 13:43 211 查看
输入代码:

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageP extends JFrame
{
public 	ImageP()
{
add(new ImagePanel());
}
public static void main(String[] args)
{
JFrame frame =new ImageP();
frame.setTitle("欢迎来到烟台大学!");
frame.setSize(300,300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ImagePanel extends JPanel
{
private ImageIcon imageIcon=new ImageIcon("haige2/yanda.jpg");
private Image image=imageIcon.getImage();

protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if(image!=null)
{
g.drawImage(image,0,0,getWidth(),getHeight(),this);
}
}
}


运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: