JPanel 图像和组件
我希望 JPanel 上有图像,并且 JPanel 上也有 JSlider 和 JRadioButton 等组件。如您所见,我从 JPanel 派生了一个类并重写了 PaintComponent 方法。这是在 JPanel 上显示图像的好方法。
public void paintComponent(Graphics g)
{
/*create image icon to get image*/
ImageIcon imageicon = new ImageIcon(imageFile); //getClass().getResource(imageFile)
Image image = imageicon.getImage();
/*Draw image on the panel*/
super.paintComponent(g);
if (image != null)
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
但是我有一些问题。当我在 ImagePanel 上添加 JSlider、JRRadioButton 或其他 JPanel 等组件时;该组件的背景保持默认状态,而不是背景图片。我不知道如何将此图像设置为该组件的背景。 请指导我。
问候
I would like to have image on my JPanels an also have components such as JSlider and JRadioButton on my JPanel. I derived a class from JPanel and overrided method paintComponent as you see. This is a good way to have image on JPanel.
public void paintComponent(Graphics g)
{
/*create image icon to get image*/
ImageIcon imageicon = new ImageIcon(imageFile); //getClass().getResource(imageFile)
Image image = imageicon.getImage();
/*Draw image on the panel*/
super.paintComponent(g);
if (image != null)
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
However I have some problems. When I add components such as JSlider, JRadioButton or another JPanel on my ImagePanel; this component's background remains as default and not background picture. I don't know how to set this image as background of this components.
please guide me.
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须将其他组件的不透明属性设置为 false。
例如:
you must set opaque properties of other component to false.
for example :
适用于许多外观和感觉,但如果您希望它与 Nimbus 一起使用,您还应该将背景颜色设置为透明:
请参阅 此问题了解更多详细信息。
Will work for many look-and-feels, but if you want it to work with Nimbus you should also set the background color to be transparent:
See this question for more details.
对于所有其他组件,
setOpaque(false)
没有帮助吗?Doesn't
setOpaque(false)
for all the other components help?