JAVA/Swing编程中drawImage()不能正常显示图片。
class MainWindow extends JFrame{
protected JPanel panel1;
MainWindow(String s){
super(s);
this.setSize(800,600);
this.setLocation(200, 100);
this.getContentPane().setBackground(Color.BLACK);
Image image1=this.getToolkit().createImage("MainWindowImage.jpg");
this.setVisible(true);
this.getContentPane().getGraphics().drawImage(image1, 200, 100, this);
}
}
初学,还请不吝赐教,多谢多谢!
------解决方案--------------------你调用setVisible方法过早,你试下把这个方法放在构造函数的最后
------解决方案--------------------随便改的
Java code
class MainWindow extends JFrame{
MainWindow(String s){
super(s);
this.setSize(800,600);
this.setLocation(200, 100);
Image image1=this.getToolkit().createImage("MainWindowImage.jpg");
this.setContentPane(new XPanel(image1));
this.setVisible(true);
}
class XPanel{
private Image image;
public XPanel(Image image){
this.image = image;
setBackground(Color.BLACK);
}
@Override public void paintComponent(Graphics g){
g.drawImage(image, 200, 100, this);
}
}
}