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

发布于 2024-10-09 18:42:28 字数 1273 浏览 4 评论 0原文

我想显示一个显示一些文本的文本区域(将显示日志行),并在其上方悬停一个动画 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();
    }

}

I want to show a textArea showing some text (will show log lines) , and have an animated gif hoovering above it. I tried the solution described here , but all I get is a grey screen. Hints?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

煞人兵器 2024-10-16 18:42:28

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

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

Try putting your animated GIF on the glass pane of your root pane:

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

困倦 2024-10-16 18:42:28

JXLayer 可以更轻松地做到这一点。查看 JXLayer 示例。

您还可以查看 XSwingX 的代码

JXLayer make easier to do that. Look at JXLayer samples.

You also can take a look at code of XSwingX

人间☆小暴躁 2024-10-16 18:42:28

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

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

Since you started with a working example, why did you remove lines of code from the example you copied?

Layered panes don't use a layout manager therefore the size of your components are (0, 0), so there is nothing to display. The setBounds(...) method in the example are there for a reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文