带有JDialog的JLayeredPane [英] JLayeredPane with a JDialog

查看:95
本文介绍了带有JDialog的JLayeredPane的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将其添加到JDialog时,无法在JLayeredPane上显示任何组件.

I am unable to make any components apear on a JLayeredPane when adding it to a JDialog.

我还无法找到一个网络资源,该资源表明可以用合理大小的代码块完成此操作.第四,每一个视线都在看索赔",然后给出了令人厌恶的长期解决方案.

I have also been unable to find a web resource that shows this may be done in a reasonably sized block of code. Every sight iv looked at "claims" this can be done, and then shows a disgustingly long solution.

我想要的是获取一个JLayered窗格,添加一个Button并将一个带有图标的JLabel放置到该窗格上.用英语,我想要一个带有图标的按钮,该按钮的文本前面有一个

What i want is to take a JLayered pane add a Button and place a JLabel with an icon in it onto this pane aswell. In english i want a button with an icon stuck in the front of its text.

那是awt Button,因为我一直无法找到一种使系统看起来像JButton的方法.

That is the awt Button as I have been unable to find a way of making a system looking swing JButton.

您能帮我解决一些更具体的问题吗?我认为我在帖子中含糊其辞.

could you help me out with something a little more specific. I think I was a littile to vague in my post.

Button button = new Button("ok");
JDialog dialog = new JDialog(null,"Windows",Dialog.ModalityType.APPLICATION_MODAL);
dialog.getLayeredPane().add(button);
dialog.pack();
dialog.setVisible(true);

推荐答案

我似乎没有任何问题...

I don't seem to have any issues...

public class TestLayeredDialog {

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

    public TestLayeredDialog() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JDialog dialog = new JDialog();
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setLayout(new BorderLayout());
                dialog.add(new MyContent());
                dialog.pack();
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);

                System.exit(0);
            }
        });
    }

    public class MyContent extends JLayeredPane {

        public MyContent() {
            JLabel label = new JLabel("Hello new world");
            label.setSize(label.getPreferredSize());
            label.setLocation(0, 0);
            add(label);

            Dimension size = getPreferredSize();

            JButton button = new JButton("Click me");
            button.setSize(button.getPreferredSize());
            button.setLocation(size.width - button.getWidth(), size.height - button.getHeight());
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.getWindowAncestor(MyContent.this).dispose();
                }
            });
            add(button);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

请记住,JLayeredPane没有布局管理器.这就是重点,您将负责管理子组件的大小和位置.

Remember, JLayeredPane DOES NOT have a layout manager. You become responsible for managing the size and position of the child components, that's the point.

已更新为新示例

public class TestLayeredDialog {

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

    public TestLayeredDialog() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JDialog dialog = new JDialog();
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setLayout(new BorderLayout());

                JLabel label = new JLabel("Hello new world");
                label.setSize(label.getPreferredSize());
                label.setLocation(0, 0);
                dialog.getLayeredPane().add(label, new Integer(1));

                dialog.setSize(100, 100);
                dialog.setLocationRelativeTo(null);
                dialog.setVisible(true);

                System.exit(0);
            }
        });
    }
}

JRootPane的分层窗格负责(除其他事项外)布置内容窗格和菜单栏.在某些情况下,它还用于显示弹出窗口之类的内容.

The layered pane of the JRootPane is responsible for (amongst other things) laying out the content pane and menu bar. It is also used (in some cases) to display things like popups.

已通读如何使用根窗格

您可以选择将组件放置在根窗格的分层窗格中.如果 然后,您应该意识到某些深度被定义为 用于特定功能,则应将深度用作 故意的.否则,您的组件可能无法与 其他.这是显示功能层及其功能的图 关系:

You can choose to put components in the root pane's layered pane. If you do, then you should be aware that certain depths are defined to be used for specific functions, and you should use the depths as intended. Otherwise, your components might not play well with the others. Here's a diagram that shows the functional layers and their relationship:

使用它,意味着您正在与屏幕上已经存在的组件竞争.

Using this, means you are competing with components already on the screen.

除非非常有充分的理由要弄乱这个组件,否则我建议您避免使用它,因为1-将来可能会更改(组件的层位置)和2-可能会干扰其他组件由Swing API使用

Unless you have VERY good reason to be messing with this component, I would suggest you avoid it as 1- It's possible to be changed in the future (the layer position of the components) and 2- It may interfere with other components used by the Swing API

这篇关于带有JDialog的JLayeredPane的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆