java swing学习ing,写了段java核心技术上的代码,为什么没有图片显示出来呢!
学习中。。。
package com.chen.jframe;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
/**
* 测试打印出图片的 类名: ImageTest
*
* @version 1.0 2012-8-04
*/
public class ImageTest {
public static void main(String[] args) {
// 创建一个启动JF的线程
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ImageFrame frame = new ImageFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/**
* image窗口
*/
class ImageFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
Imagecomponet imgc;
public static final int DEFALUT_WIDTH = 600;
public static final int DEFALUT_HIGHT = 400;
public ImageFrame() {
imgc = new Imagecomponet();
// 添加组件
this.add(imgc);
this.setTitle("ImageTest");
this.setSize(DEFALUT_WIDTH, DEFALUT_HIGHT);
}
}
/**
*image组件
*/
class Imagecomponet extends JComponent {
private static final long serialVersionUID = 1L;
private Image img;
public Imagecomponet() {
try {
img = ImageIO.read(new File("img/sade.jpg"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void paintComponet(Graphics g) {
super.paintComponent(g);
if (img == null) {
return;
}
int imgWeight=img.getWidth(this);
int imgHight=img.getWidth(this);
g.drawImage(img, 0, 0,imgWeight, imgHight,this);
}
}