问题
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