[英]Adding Image to JPanel directly
我正在一个设计游戏的项目中,我想知道是否可以在不将图像添加到jLabel的情况下直接将图像添加到JPanel,然后将标签添加到面板。
您可以这样尝试:
class Sample extends JPanel {
BufferedImage image;
Pseudo(BufferedImage image) {
this.image = image;
// or load it in this class
setLayout(null);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int x =
int y =
g.drawImage(image, x, y, this);
}
}
这会将图像直接添加到JPanel。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.