2016-10-31 58 views
0

單擊按鈕後,我有我的代碼,以便新面板顯示新組件,但是,只需單擊一個按鈕,它就不會顯示。我的面板怎麼沒有出現?

我相信我已經表現出來了,但我仍然不確定它爲什麼沒有顯示出來。

這裏是我的代碼:

  JPanel panelTest = new JPanel(); 
      JPanel centerPanel = new JPanel(new GridLayout(0, 2, 5, 2)); 

      JLabel firstNameLabel = new JLabel("First Name: ", JLabel.RIGHT); 
      JLabel lastNameLabel = new JLabel("Last Name: ", JLabel.RIGHT); 

      JTextField firstNameField = new JTextField(10); 
      JTextField lastNameField = new JTextField(10); 

      centerPanel.add(firstNameLabel); 
      centerPanel.add(firstNameField); 
      centerPanel.add(lastNameLabel); 
      centerPanel.add(lastNameField); 

      panelTest.add(centerPanel); 

究竟如何,我應該讓我的面板上顯示一個按鈕的點擊?因爲一旦我點擊它,什麼都沒有顯示。其他代碼適用於按鈕,我不能讓這個面板顯示文本字段和所有。

感謝您的幫助。

+0

你需要在你的主框架中添加'panelTest'。 – Antoniossss

+2

沒有[SSCCE](http://sscce.org)我們不能準確地告訴你的問題是什麼。 –

回答

0

嘗試:

frame.validate(); 
frame.repaint(); 

,其中框架是你的JFrame你的名字。我有這個問題,並且必須在我將JFrame設置爲可見時向JFrame添加內容時執行此操作。

0

據我所知,你沒有說話JFrame舉行你的小組。 創建一個JFrame,然後添加你的面板jframe.add(panelTest);

相關問題