Java Swing:显示悬停在另一个面板上的透明面板

Java Swing:显示悬停在另一个面板上的透明面板,java,swing,jlayeredpane,Java,Swing,Jlayeredpane,我想显示一个文本区域,显示一些文本(将显示日志行),并在其上方有一个动画gif动画。我尝试了描述的解决方案,但我得到的只是一个灰色屏幕。暗示 public class TestLayeredPanes { private JFrame frame = new JFrame(); private JLayeredPane lpane = new JLayeredPane(); public TestLayeredPanes() { frame.setPr

我想显示一个文本区域,显示一些文本(将显示日志行),并在其上方有一个动画gif动画。我尝试了描述的解决方案,但我得到的只是一个灰色屏幕。暗示

public class TestLayeredPanes {

    private JFrame frame = new JFrame();
    private JLayeredPane lpane = new JLayeredPane();

    public TestLayeredPanes() {
        frame.setPreferredSize(new Dimension(600, 400));
        frame.setLayout(new BorderLayout());
        frame.add(lpane, BorderLayout.CENTER);

        //Build the animated icon
        JLabel buildingIcon = new JLabel();
        buildingIcon.setIcon(new ImageIcon(this.getClass().getResource(
                "/com/ct/tasks/cmviewer/gui/progress_bar.gif")));       
        JPanel iconPanel = new JPanel();
        iconPanel.add(buildingIcon);

        //Build the textArea
        JTextArea textLog = new JTextArea("Say something");     
        JPanel textPanel = new JPanel();
        textPanel.add(new JScrollPane(textLog));

        //Add the panels to the layered pane
        lpane.add(textPanel, 0);
        lpane.add(iconPanel, 1);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestLayeredPanes();
    }

}

尝试将动画GIF放在根窗格的玻璃窗格上:


JXLayer使这一点更容易做到。看看样品


您还可以查看

的代码,因为您从一个工作示例开始,为什么要从复制的示例中删除代码行


分层窗格不使用布局管理器,因此组件的大小为(0,0),因此无需显示任何内容。示例中的setBounds(…)方法是有原因的。

酷,我会研究一下。看起来不错!它起作用了,但我仍然不知道为什么另一个解决方案不起作用。