java JFrame 中的背景图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2645452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:11:09  来源:igfitidea点击:

Background image in a JFrame

javaswingjframebackground-image

提问by thepandaatemyface

This question has been asked a lot but everywhere the answers fall short. I can get a JFrame to display a background image just fine by extending JPanel and overriding paintComponent, like so:

这个问题被问了很多,但到处都没有答案。我可以通过扩展 JPanel 和覆盖 PaintComponent 来获得一个 JFrame 来显示背景图像,如下所示:

class BackgroundPanel extends JPanel {
    private ImageIcon imageIcon;
    public BackgroundPanel() {
        this.imageIcon = Icons.getIcon("foo");
  }

    @Override
    protected void paintComponent(Graphics g) {
       super.paintComponent(g);
        g.drawImage(imageIcon.getImage(), 0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight(),this);
    }
}

But now, how do you add a component on top of that background? When I go

但是现在,您如何在该背景之上添加组件?当我去

JFrame w = new JFrame() ;
Container cp = w.getContentPane();
cp.setLayout(null);

BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);

JPanel b = new JPanel();
b.setSize(new Dimension(30, 40));
b.setBackground(Color.red);
cp.add(b);
w.pack()
w.setVisible(true)

It shows the little red square (or any other component) and not the background, but when I remove cp.setLayout(null);, the background shows up but not my other component. I'm guessing this has something to do with the paintComponent not being called by the null LayoutManager, but I'm not at all familiar with how LayoutManagers work (this is a project for college and the assignment specifically says not to use a LayoutManager).

它显示小红色方块(或任何其他组件)而不是背景,但是当我删除时cp.setLayout(null);,背景显示但不是我的其他组件。我猜这与没有被 null LayoutManager 调用的paintComponent有关,但我一点也不熟悉LayoutManagers的工作原理(这是一个大学项目,作业特别说不要使用LayoutManager) .

When i make the image the background has to display null (and so, transparant (??)) the red square shows up so it might be that the background is actually above my other components.

当我制作图像时,背景必须显示为空(因此,透明 (??))红色方块显示,因此背景可能实际上位于我的其他组件上方。

Does anyone anyone have any ideas?

有没有人有任何想法?

Thanks

谢谢

回答by Nate

When using nulllayout (and you almost nevershould) you have to supply a bounds for every component, otherwise it defaults to (0 x,0 y,0 width,0 height) and the component won't display.

使用null布局时(你几乎从来不应该)你必须为每个组件提供一个边界,否则它默认为 (0 x,0 y,0 width,0 height) 并且组件不会显示。

BackgroundPanel bg = new BackgroundPanel();
cp.add(bg);

isn't supplying a bounds. You'll need to do something like:

不提供边界。您需要执行以下操作:

BackgroundPanel bg = new BackgroundPanel();
bg.setBounds(100, 100, 100, 100);
cp.add(bg);

Which would make bgsize 100 x 100 and place it at 100 x, 100 y on the frame.

这将使bg大小为 100 x 100 并将其放置在框架上的 100 x, 100 y 处。

回答by justkt

Look in the documentation on the Root Panefor all the information you need. Note the availability of the layered pane and the glass pane as well as the content pane.

查看根窗格上的文档以获取您需要的所有信息。请注意分层窗格和玻璃窗格以及内容窗格的可用性。

回答by camickr

By default all components have a 0 size. Just because you do some painting on a component doesn't give the component a size. You are still responsible for setting the size. That is why you should always use a layout manager. It looks after all this size stuff for you so you don't have to worry.

默认情况下,所有组件的大小均为 0。仅仅因为您在组件上进行了一些绘制并不会给出组件的大小。您仍然负责设置大小。这就是为什么您应该始终使用布局管理器的原因。它会为您处理所有这些尺寸的东西,因此您不必担心。

I don't know why newbies always think they can't use a layout manager. Yes it takes a couple of more minutes to learn, but it saves you a lot of grief in the long run.

我不知道为什么新手总是认为他们不能使用布局管理器。是的,学习需要多花几分钟的时间,但从长远来看,它可以为您省去很多麻烦。

Background Panelshows a couple of approaches. Again they both assume you use a layout manager, so you may need to set the size manually.

背景面板显示了几种方法。同样,他们都假设您使用布局管理器,因此您可能需要手动设置大小。