[英]How to set the previous jframe visible when current jframe is disposed
我正在制作一個Java gui項目,它由兩個框架組成。
問題是,當我從第一幀調用第二幀時,已將其設置為使第一幀可見性設置為false。 問題是如何使用第二幀中的按鈕使第一幀再次可見。
我應該放棄這種方法,而是創建一個新的jpanel嗎??? jpanel是否具有與jframe類似的功能?
考慮使用CardLayout
。 這樣,您可以通過多個UI進行切換,而無需其他框架。 這是使用方法。
編輯:作為紀堯姆張貼在他的評論中, 這從安德魯的回答也介紹了如何使用的布局。
EDIT2:
當您請求有關我的最新帖子的更多信息時,此類課程的外觀如下:
import javax.swing.JFrame;
public abstract class MyFrameManager {
static private JFrame startFrame,
anotherFrame,
justAnotherFrame;
static public synchronized JFrame getStartFrame()
{
if(startFrame == null)
{
//frame isnt initialized, lets do it
startFrame = new JFrame();
startFrame.setSize(42, 42);
//...
}
return startFrame;
}
static public synchronized JFrame getAnotherFrame()
{
if(anotherFrame == null)
{
//same as above, init it
}
return anotherFrame;
}
static public synchronized JFrame getJustAnotherFrame()
{
//same again
return justAnotherFrame;
}
public static void main(String[] args) {
//let's test!
JFrame start = MyFrameManager.getStartFrame();
start.setVisible(true);
//want another window
JFrame another = MyFrameManager.getAnotherFrame();
another.setVisible(true);
//oh, doenst want start anymore
start.setVisible(false);
}
}
這樣,您只需實例化每個JFrame
一次,但始終可以通過管理器類訪問它們。 之后,您對他們的處理是您的決定。
我還使它成為線程安全的,這對於單例來說至關重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.