我想知道如何在 Java 中将图像设置为应用程序的背景。我知道在 android 中这是非常直接的,并且 windows builder pro 有很多用于构建 Java gui 的惊人工具,所以想知道是否有办法可以做到这一点?提前致谢!我的应用程序看起来很糟糕,呈灰色......
最佳答案
您无法准确地将背景设置为图像。你所要做的就是在绘画过程中将图像绘制在图形上。因此,您需要继承 JPanel 并重写 PaintComponent() 方法,并在那里绘制图像。
public class ImagePanel extends JPanel {
private Image bgImage;
public Image getBackgroundImage() {
return this.bgImage;
}
public void setBackgroundImage(Image image) {
this.bgImage = image;
}
protected paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage( bgImage, 0, 0, bgImage.getWidth(null), bgImage.getHeight(null), null );
}
}
关于java - 如何在 eclipse 或 windows builder pro 中将图像设置为 Java 应用程序的背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8773608/