2014-11-25 66 views
4

我正在使用Java的桌面應用程序。我放置了一個滾動窗格並在其中放置了一個面板,以添加名爲name的標籤和一個空的TextField以及一個名爲more的按鈕。我想要的是當按鈕被點擊時,新的Label和TextField應該出現在第一個Label和TextField下面,但是什麼也沒有出現。這裏是我的按鈕代碼:無法將組件添加到java中的scrollPane中的面板

private void moreActionPerformed(java.awt.event.ActionEvent evt) {          
    JLabel nameLabel = new JLabel("Name :"); 
    JPanel panel = new JPanel(); 
    i++; 
    nameLabel.setBounds(
     child_namelabel.getX(), child_namelabel.getY() + 35 * i, 
     child_namelabel.getWidth(), child_namelabel.getHeight()); 
    panel.add(nameLabel); 
    children.add(panel); 

} 

回答

0

從Java的API:

This method changes layout-related information, and therefore, invalidates the component hierarchy. 
If the container has already been displayed, the hierarchy must be validated thereafter in order to 
display the added component. 

所以你撥打:

children.validate(); 

在你的方法結束。