简体   繁体   English

程序如何在不结束程序的情况下关闭GUI

[英]How Can A Program Close A GUI Without The Program Ending

Title says it all, before anyone says that there is a thread about closing a GUI without the program end then hold on a second. 标题说了这一切,然后才有人说在没有程序结束的情况下关闭GUI的线程,然后稍等片刻。

My question is that, how can the PROGRAM close a GUI and not the user so it can progress onto another frame. 我的问题是,程序如何关闭GUI而不关闭用户,以便它可以前进到另一个框架。

If this question below is about a Swing GUI: 如果以下问题与Swing GUI有关:

My question is that, how can the PROGRAM close a GUI and not the user so it can progress onto another frame. 我的问题是,程序如何关闭GUI而不关闭用户,以便它可以前进到另一个框架。

Then this question can be answered thusly: 这样可以回答这个问题:

  • Set the JFrames default close operation property to JFrame.DO_NOTHING_ON_CLOSE . JFrame.DO_NOTHING_ON_CLOSE默认关闭操作属性设置为JFrame.DO_NOTHING_ON_CLOSE
  • A program can close any window by calling close() or setVisible(false) on the window. 程序可以通过在窗口上调用close()setVisible(false)来关闭任何窗口。

ie,

myJFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

But having said that, the question in fact should be answered: 但话虽如此,实际上应该回答这个问题:

  • Don't do this as this is a bad program design. 不要这样做,因为这是不好的程序设计。 No user wants windows flung at him. 没有用户希望窗户向他扑来。
  • Instead have one main JFrame window open and rather than open and close windows, change views in the window by swapping JPanels, each holding its own simple or complex GUI. 而是打开一个主JFrame窗口,而不是打开和关闭窗口,而是通过交换JPanels来更改窗口中的视图 ,每个JPanels拥有自己的简单或复杂的GUI。
  • A CardLayout can help you do this easily. CardLayout可以帮助您轻松完成此操作。

Again, note that your question as originally stated is very incomplete, and in the future, you will want to tell us any and all relevant libraries that are involved, here the Swing library since the question is likely essentially a Swing question. 同样,请注意,您最初提出的问题非常不完整,将来,您将想告诉我们所涉及的所有相关库,这里是Swing库,因为该问题本质上可能是Swing问题。 You will also want to show some code and explain exactly where you might be stuck. 您还将需要显示一些代码并确切说明您可能会遇到的问题。 Usually the better the question, the better the answers you'll see. 通常,问题越好,您看到的答案就越好。 For instance, with more information and code, we could show you some code to get you started with a CardLayout. 例如,借助更多的信息和代码,我们可以向您展示一些代码,以帮助您开始使用CardLayout。

java.awt.Window aTopLevelGUI; // Normally referenced as a JWindow, but this is what impls setVisible(boolean)

// To close the window:
aTopLevelGUI.setVisible(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM