首次将图像绘制到java JPanel时它不起作用?
代码如下所示:
class MyPanel extends JPanel{
Image myImage=null;
boolean isFirst=true;
@Override
public void paint(Graphics g) {
super.paint(g);
if(!isFirst) return;
isFirst=false;
g.drawImage(myImage, 350, 200, 50, 50, this);
}
public MyPanel() {
myImage=Toolkit.getDefaultToolkit().getImage(
Panel.class.getResource("/MyImg.png"));
}
}
这是MyPanel的电话: public class App扩展JFrame { public App(){ MyPanel mp = new MyPanel(); this.add(MP); } }
我已经解决了这个问题。它看起来像这样:
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(myImage, 350, 200, 50, 50, this);
}