Java:在另一个组件之上设置一个组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5097442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java : set a Component on top of another
提问by sajad
I am writing a program in java. I have a main JPanel that has two JPanel and one Canvas added on it. I aim to resize the Canvas while running the program. When I maximized the Canvas i want it to be always on top of the other component.
How can I set this property for my Canvas?
我正在用java编写一个程序。我有一个主 JPanel,上面添加了两个 JPanel 和一个 Canvas。我的目标是在运行程序时调整画布的大小。当我最大化 Canvas 时,我希望它始终位于其他组件的顶部。
如何为我的 Canvas 设置此属性?
回答by sstendal
You could replace your main JPanel with a JLayeredPanel. A layered panel will let you specify that some child components should be layered above other child components.
您可以用 JLayeredPanel 替换主 JPanel。分层面板可让您指定某些子组件应位于其他子组件之上。
I.e.:
IE:
JLayeredPane pane = new JLayeredPane();
JLabel ontop = new JLabel("On top");
JLabel behind = new JLabel("Behind");
pane.add(ontop, 2, 0);
pane.add(behind, 1, 0);
回答by user489041
Make your main JPanel a JLayeredPane
使您的主 JPanel 成为JLayeredPane
Then you can set the layer of the other components with setLayer(Component c, int layer)
thus allowing them to overlap.
然后您可以设置其他组件的层,setLayer(Component c, int layer)
从而允许它们重叠。