Java JFrame 更换屏幕

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18857445/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:40:44  来源:igfitidea点击:

JFrame changing screens

javaswingjframe

提问by Arc

I'm wondering how to change screens in a JFrame. For example, changing from the starting screen to a different screen. So you have an assortment of buttons, labels, trees, etc on one screen, as the user clicks a button a different layout appears.

我想知道如何在 JFrame 中更改屏幕。例如,从起始屏幕更改为不同的屏幕。所以你在一个屏幕上有各种各样的按钮、标签、树等,当用户点击一个按钮时,会出现不同的布局。

Would the 'setVisible(false) and setVisible(true)' do the trick?

'setVisible(false) 和 setVisible(true)' 会起作用吗?

采纳答案by kwikness

You've got it! Create separate JFrame instances for each of your frames:

你明白了!为每个框架创建单独的 JFrame 实例:

JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();

//populate your frames with stuff

frame1.setVisible(false);
frame2.setVisible(true);

On a side note, you'll want to make sure to use setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)on any secondary frames to prevent your application from terminating if a user closes a secondary frame.

附带说明一下,您需要确保在任何辅助框架上使用setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)以防止您的应用程序在用户关闭辅助框架时终止。

All that being said, you can also use multiple JPanel instances inside of the same JFrame instead of creating multiple JFrame instances. This way, all the action of your application will take place in one window.

尽管如此,您还可以在同一个 JFrame 内使用多个 JPanel 实例,而不是创建多个 JFrame 实例。这样,您的应用程序的所有操作都将在一个窗口中进行。

I would strongly recommend giving this a read through: http://docs.oracle.com/javase/tutorial/uiswing/

我强烈建议通读一遍:http: //docs.oracle.com/javase/tutorial/uiswing/