2011-11-28 65 views
0

我創建了一個框架並將其擴展狀態設置爲JFrame.MAXIMIZED_BOTH。該窗口在啓動時顯示爲最大化,但在按下「還原」按鈕後,它將調整爲只有上半部分的零大小的窗口,其中包含最小化,最大化和關閉按鈕。之後,我可以手動調整窗口大小,並顯示內容。最小化Swing中的窗口

我希望我的窗口在啓動時最大化,但不想在恢復按鈕點擊後丟失它。

下面是代碼:

public class MyFrame extends JFrame { 
     public MyFrame() { 
     //... 
     setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);   
     setVisible(true); 
     } 
} 

回答

2

你應該叫pack()setVisible(true)之前,爲了確保最佳尺寸正確計算。我認爲恢復應該正常工作。

1

也許你應該試試這個:

public class MyFrame extends JFrame { 
     public MyFrame() { 
     //... 
     setSize(500,400); // Watever size you want to set. 
     setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);   
     setVisible(true); 
     } 
} 
+1

'setSize'正確的想法,錯誤的方法。查看AKJ的答案。 –

+0

你說得對。我剛剛遇到了正在設置的這個屬性。 –