是否有可能從JPanel
類獲得父對象JFrame
?
其實我試圖做一個使用NetBeans的GUI。GUI有一個框架和兩個面板。
1)登錄面板(具有兩個文本字段和按鈕)
2)第二面板
當JFrame的加載,我在它添加LoginPanel
最初。
從JPanel對象獲取父框架對象
public ParentJFrame() { //in constructor
initComponents();
this.setLayout(new BorderLayout());
this.setBounds(300,300, 300, 300);
this.getContentPane().add(new LoginPanel());
}
點擊(的LoginPanel)按鈕後,我試圖從JFrame中刪除LoginPanel
和添加SecondPanel
。
現在我正在刪除LoginPanel
並在LoginPanel
類中添加SecondPanel
,我可以訪問username , password
字段,但是在這裏我無法獲取必須從中刪除此組件的JFrame對象。
如果我在ParentJFrameClass
試試這個,那麼我不可能訪問username , password
字段的值。 (因爲這是netbeans提供的結構)
那麼我現在該做什麼?什麼是在這種情況下
我希望我已經詳細解釋了這個問題,但在情況下,如果有什麼不太明白,請讓我知道
高手請幫忙
[SwingUtilities.windowForComponent(..)(http://docs.oracle.com/javase/7/docs/api/javax /swing/SwingUtilities.html#windowForComponent(java.awt.Component)) – nachokk
請問您能否詳細說明.. – Despicable
您應該使用'CardLayout'在[CardLayoutManager]中進行了解更多(http://docs.oracl e.com/javase/tutorial/uiswing/layout/card.html) – nachokk