简体   繁体   English

JFrame中的背景图片

[英]Background image in a JFrame

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); 它显示了红色的小方块(或任何其他组件),而不是背景,但是当我删除cp.setLayout(null);时,它显示为红色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). 我猜想这与没有由null LayoutManager调用的paintComponent有关,但是我完全不熟悉LayoutManager的工作方式(这是一个专为大学设计的项目,作业明确指出不要使用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 谢谢

When using null layout (and you almost never should) 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 bg size 100 x 100 and place it at 100 x, 100 y on the frame. 这将使bg大小为100 x 100,并将其放置在框架上的100 x,100 y处。

Look in the documentation on the Root Pane for all the information you need. 根窗格上的文档中查找所需的所有信息。 Note the availability of the layered pane and the glass pane as well as the content pane. 注意分层窗格和玻璃窗格以及内容窗格的可用性。

By default all components have a 0 size. 默认情况下,所有组件的大小均为0。 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. 它会为您照顾所有这些大小的东西,因此您不必担心。

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 Panel shows a couple of approaches. 背景面板显示了几种方法。 Again they both assume you use a layout manager, so you may need to set the size manually. 同样,它们都假定您使用布局管理器,因此您可能需要手动设置大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM