带有JDialog的JLayeredPane

在将JLayeredPane添加到JDialog时,我无法在JLayeredPane上创建任何组件。

我也无法找到一个显示可以在合理大小的代码块中完成此操作的Web资源。 每一个视线iv都看着“索赔”,这可以做到,然后显示一个令人作呕的长期解决方案。

我想要的是采取JLayered窗格添加一个Button并将带有图标的JLabel放入此窗格中。 在英语中我想要一个带有图标的按钮卡在其文本的前面。

这就是awt Button因为我一直无法找到一种让系统看起来摇摆JButton的方法。

编辑:你能帮我解决一些更具体的问题。 在我的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); 

我似乎没有任何问题……

 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没有布局管理器。 您负责管理子组件的大小和位置,这就是重点。

更新了新示例

在此处输入图像描述

 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的分层窗格负责(除其他事项外)布局内容窗格和菜单栏。 它也用于(在某些情况下)显示弹出窗口之类的东西。

在此处输入图像描述

阅读如何使用根窗格

您可以选择将组件放在根窗格的分层窗格中。 如果这样做,那么您应该知道某些深度被定义为用于特定function,您应该按预期使用深度。 否则,您的组件可能无法与其他组件一起使用。 这是一个显示function层及其关系的图表:

使用此function,意味着您正在与屏幕上已有的组件竞争。

除非你有充分的理由去弄乱这个组件,否则我建议你避免使用它 – 将来可能会被改变(组件的层位置)和2-它可能会干扰其他组件使用Swing API

此示例似乎与添加到构造函数中的以下行一起使用:

 this.addMouseListener(new MouseHandler(this)); this.add(new JLabel("Label")); this.add(new JButton(UIManager.getIcon("html.pendingImage"))); 

在此处输入图像描述