Java 将 JLayeredPane 添加到 JPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1764115/
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-08-12 22:24:55  来源:igfitidea点击:

adding JLayeredPane to JPanel

javajpaneljlayeredpane

提问by asawilliams

I am trying to add a JLayeredPane to a JPanel and then add an image (JLabel icon) and a button to the JLayeredPane, but neither show up. I've tested the image without the button and the layeredpane so I know that works. Here is some of the code I am using. Is there something I am missing or doing wrong?

我正在尝试将 JLayeredPane 添加到 JPanel,然后将图像(JLabel 图标)和按钮添加到 JLayeredPane,但都没有显示。我已经在没有按钮和 layeredpane 的情况下测试了图像,所以我知道这是可行的。这是我正在使用的一些代码。我有什么遗漏或做错了吗?


public class MyClass extends JPanel 
{
    private JLayeredPane layeredPane;
    private JLabel imageContainer = new JLabel();
    private JButton info = new JButton("i");

    MyClass(ImageIcon image)
    {
        super();

        this.imageContainer.setIcon(image);

        this.layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(300, 300));
        layeredPane.add(imageContainer, new Integer(50));
        layeredPane.add(info, new Integer(100));

        this.add(layeredPane);
    }
}       

采纳答案by OscarRyz

From the tutorial

教程

By default a layered pane has no layout manager. This means that you typically have to write the code that positions and sizes the components you put in a layered pane.

默认情况下,分层窗格没有布局管理器。这意味着您通常必须编写代码来定位和调整放置在分层窗格中的组件。

See the changes to your code:

查看对代码的更改:

import java.awt.*;
import javax.swing.*;
public class MyClass extends JPanel {
    private JLayeredPane layeredPane;
    private JLabel imageContainer = new JLabel();
    private JButton info = new JButton("i");

    MyClass(ImageIcon image) {
        super();

        this.imageContainer.setIcon(image);

        this.layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(300, 300));
        layeredPane.add(imageContainer, new Integer(50));
        layeredPane.add(info, new Integer(100));
        this.add(layeredPane);
        // CHANGED CODE
        // Manually set layout the components. 
        imageContainer.setBounds( 0, 0,  
                                  image.getIconWidth(),
                                  image.getIconHeight() ); 
        info.setBounds( 200, 00,  50, 40 );
    }
    public static void main( String [] args ) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new MyClass( new ImageIcon("logo.png")  ) );
        frame.pack();
        frame.setVisible( true );
    }
}       

Additional notes:

补充说明:

1) It is better ( in my opinion ) to put the opening brace in the same line. That's how most Java code looks like.

1)最好(在我看来)将左括号放在同一行。这就是大多数 Java 代码的样子。

2) Avoid inheriting from JPanel ( or any other component ) if you don't are not really creating a subclass. You can use it directly without having to inherit ( unless you're indeed creating a new component.

2)如果您不是真正创建子类,请避免从 JPanel(或任何其他组件)继承。您可以直接使用它而无需继承(除非您确实要创建新组件。

回答by user101884

JLayeredPane has a null layout manager by default, so in your example you'll need to set the location and size of the child components. You can set a layout manager on the JLayeredPane, but that will most likely negate the layered rendering I'm guessing you want, since you're using a layered pane.

JLayeredPane 默认有一个空布局管理器,因此在您的示例中,您需要设置子组件的位置和大小。您可以在 JLayeredPane 上设置布局管理器,但这很可能会否定我猜您想要的分层渲染,因为您使用的是分层窗格。