Java : set a Component on top of another

坚强是说给别人听的谎言 提交于 2019-12-10 14:15:30

问题


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?


回答1:


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.

I.e.:

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



回答2:


Make your main JPanel a JLayeredPane

Then you can set the layer of the other components with setLayer(Component c, int layer) thus allowing them to overlap.



来源:https://stackoverflow.com/questions/5097442/java-set-a-component-on-top-of-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!