想在java中学习windows编程,想将图像显示到一个框架中。这是问题代码:
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("hello world");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
Graphics graph = frame.getGraphics();
BufferedImage image = ImageIO.read(new File("data/image.jpg"));
graph.drawImage(image, 0,0,frame);
frame.pack();
frame.setVisible(true);
}
我已经看到一些成功的示例子类化 Component 类并在paint方法中调用 Graphics.DrawImage 方法。为什么非要那样做,就不能直接抓取frame关联的Graphics对象,直接画图吗?