【问题标题】:Swing: Showing a transparent Panel hovering over another PanelSwing:显示一个透明的面板悬停在另一个面板上
【发布时间】:2011-06-02 18:58:45
【问题描述】:

我想显示一个 textArea 显示一些文本(将显示日志行),并在其上方放置一个动画 gif。我尝试了here 描述的解决方案,但我得到的只是一个灰屏。提示?

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();
    }

}

【问题讨论】:

    标签: java swing jlayeredpane


    【解决方案1】:

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

    http://download.oracle.com/javase/tutorial/uiswing/components/rootpane.html

    【讨论】:

    • 它有效,但我仍然不知道为什么其他解决方案没有。
    【解决方案2】:

    JXLayer 更容易做到这一点。查看JXLayer 示例。

    你也可以看看XSwingX的代码

    【讨论】:

    • 酷,我会调查的。看起来不错!
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2010-10-25
      • 1970-01-01
      • 2012-03-17
      • 2012-11-05
      • 2013-04-07
      • 2014-03-15
      • 2013-10-15
      • 2018-06-21
      • 1970-01-01
      相关资源
      最近更新 更多