Java - 具有背景图像和正常功能的JPanel [英] Java - JPanel with background image AND normal functionality
问题描述
我一直在四处寻找能够拥有图像背景的 JPanel
。我知道有很多信息需要这个(示例1 和示例2 )。这些都不是我正在寻找的。 p>
我正在寻找的是一种只需添加功能的方法或者在保持其他所有内容的同时,在 JPanel
上设置背景图像的功能。我现在遇到的问题是我有一些扩展 JPanel
的类,当我更改它们以扩展我的新 ImagePanel $ c时$ c> class,构造函数失败(显然,参数不同)。我有其中一个文件的构造函数
super(new BorderLayout());
,到目前为止,我能让它运行的唯一方法就是抛弃布局,或背景图片。我不能让两者一起工作。
这是最近的绝望尝试:
import java.awt。 *;
import javax.swing.JPanel;
class ImagePanel扩展JPanel {
private Image img;
public ImagePanel(Image img,LayoutManager layout){
this.img = img;
尺寸大小=新尺寸(img.getWidth(null),img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
super.setLayout(布局);
}
public void paintComponent(Graphics g){
g.drawImage(img,0,0,null);
}
}
如果有人能帮助我,或指向我正在寻找的东西,这将是伟大的。我不需要任何超级疯狂的功能,如背景缩放或类似的东西。我只想要一个图像作为背景,并让它以其他所有方式保持其功能。
谢谢。
如果您愿意接受建议,可以考虑 JLayeredPane
。
这些容器有层,每层layer能够存储组件。
所以,你可能有一个 JPanel
,或者 JLabel
就此而言,将您的背景图片放在底层,并在另一个 JPanel
上放置更高层。
如果你想坚持 ImagePanel
,你可能也应该调用 super.paintComponent
。
class ImagePanel扩展JPanel {
...
public void paintComponent(Graphics g ){
super.paintComponent(g);
g.drawImage(img,0,0,this);
}
}
I've been looking around and working on making a JPanel
able to have an image background. I know there is a ton of information reguarding this ( example 1 and example 2 ). Neither of these are exactly what I'm looking for.
What I am looking for, is a way to just add a function or the functionality to set a background image on a JPanel
while maintaining everything else. The problem that I have right now is that I have a few classes that extend JPanel
and when I change them to extend my new ImagePanel
class, the constructor fails (obviously, the parameters are different). I have the constructor for one of those files going super( new BorderLayout() );
and the only way I can get it to function so far is to ditch either the layout, or the background image. I can't get both to work together.
Here's the latest desperate attempt:
import java.awt.*;
import javax.swing.JPanel;
class ImagePanel extends JPanel {
private Image img;
public ImagePanel(Image img, LayoutManager layout) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
super.setLayout(layout);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
If anyone could help me out, or point me towards what I'm looking for, that would be great. I don't need any super crazy functionality like background scaling or anything like that. I just want an image to be the background, and for it to maintain its functionality in all other ways.
Thanks.
If you're open to suggestions, you might consider a JLayeredPane
.
These containers have layers, and each layer is capable of storing components.
So, you may have a JPanel
, or JLabel
for that matter, with your background image on the bottom layer, and place whatever else on another JPanel
, in a higher layer.
If you want to stick to your ImagePanel
, you probably should call super.paintComponent
as well.
class ImagePanel extends JPanel {
...
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
这篇关于Java - 具有背景图像和正常功能的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!