How to move from one Panel to another in a single frame without using CardLayout?

与世无争的帅哥 提交于 2019-12-04 06:37:10

问题


I have three Panels and a single Frame in my program. I want to close/hide the current panel and show/activate the next panel. I am designing a game, hence I do not want to use CardLayout.

private void select() {
    if (currentChoice == 0) {
        f.remove(gpanel1);
        gpanel = new GamePanel();
        f.add(gpanel);
    }
}

Here f is the frame object. gpanel1 and gpanel are the initialized panel objects (constructors have already been called from the frame class).

How do I hide the old panel and move to the next panel? Is there an alternative to CardLayout?


回答1:


To change the panel in JFrame use these methods

f.setContentPane(gpanel);
f.pack();

setContentPane() removes current panel and replaces it with the provided panel.



来源:https://stackoverflow.com/questions/31905971/how-to-move-from-one-panel-to-another-in-a-single-frame-without-using-cardlayout

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