我试图重叠两个相同大小的 JPanel(此处为“pseudo”和“svg”)。 JPanel 伪位于 svg 后面。 JButton 应该在 svg 前面伪移动。结果是centerPanel.moveToFront(pseudo)不起作用。我错过了什么吗?
centerPanel = new JLayeredPane();
centerPanel.setLayout(new BorderLayout());
centerPanel.add(pseudo, BorderLayout.CENTER, -1);
centerPanel.add(svg, BorderLayout.CENTER, 0);
view3D = new JButton("View 3D");
view3D.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
centerPanel.moveToFront(pseudo);
}
});
最佳答案
The JButton is supposed to move pseudo in front of svg
那么您应该使用CardLayout
。 CardLayout
是一种布局管理器,它允许 2 个(或更多)组件占据相同的空间,但一次只有一个组件可见。
阅读 Swing 教程中关于 How to Use CardLayout 的部分了解更多信息和帮助您入门的工作示例
关于java - 尝试在 JLayeredPane 中重叠两个相同大小的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490361/