2012-09-06 4 views
2

모든 시각적 빌더를 사용하여 디자인하는 동안. 런타임에 일부 구성 요소를 추가하고 싶습니다. 디자인 타임에 jpanel을 추가했습니다. jpanel에서 런타임에 버튼을 추가하고 싶습니다. 이것은 생성 된 코드이고 내가 추가 한 코드입니다. 내가 실행할 때 단추가 보이지 않습니다.런타임시 netbeans에서 구성 요소 생성

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package main; 

import javax.swing.JButton; 

/** 
    * 
* @author FirmView 
*/ 
public class NewJFrame extends javax.swing.JFrame { 

/** 
* Creates new form NewJFrame 
*/ 
public NewJFrame() { 
    initComponents(); 
} 

private void addGUIComponents(){ 
    JButton b = new JButton("Click Me"); 
    jPanel1.add(b); 
} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jPanel1 = new javax.swing.JPanel(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 
    jPanel1.setLayout(jPanel1Layout); 
    jPanel1Layout.setHorizontalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 341, Short.MAX_VALUE) 
    ); 
    jPanel1Layout.setVerticalGroup(
     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 83, Short.MAX_VALUE) 
    ); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(26, 26, 26) 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(33, Short.MAX_VALUE)) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGroup(layout.createSequentialGroup() 
      .addGap(41, 41, 41) 
      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
      .addContainerGap(176, Short.MAX_VALUE)) 
    ); 

    pack(); 
}// </editor-fold> 

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* 
    * Set the Nimbus look and feel 
    */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* 
    * If Nimbus (introduced in Java SE 6) is not available, stay with the 
    * default look and feel. For details see 
    * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* 
    * Create and display the form 
    */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 

     public void run() { 
      NewJFrame f = new NewJFrame(); 
      f.addGUIComponents(); 
      f.show(); 
     } 
    }); 
} 
// Variables declaration - do not modify 
private javax.swing.JPanel jPanel1; 
// End of variables declaration 

}

+0

내가 왜 보이지 않는지 알지만 질문은 무엇입니까? :) –

+0

@matheszabi 표시 방법 – FirmView

+0

f.addGUIComponents(); f.pack(); f.show(); 그것을 해결해야합니다, 아마도 당신은 GroupLayout에 대한 제약이 필요합니다 –

답변

1

그런 구성 요소는 나중에 revalidate()으로 전화해야합니다.

+0

+1 제안 – FirmView

0

문제는 당신이 인 JPanel 클래스를 만드는 다른 인 JPanel에 버튼을 추가 initComponent()에 JPanel의 객체 만들기 다음 있습니다 더 나은 addGUIComponents()

에서 객체이다 a Singleton ... 같은 JPanel 개체가 있습니다.

+0

아니요, 그는 추가 중입니다. 같은 jPanel1 = 새로운 javax.swing.JPanel(); initComponents() –

+0

에서 생성되었지만 그의 ContentPane에 jPanel1이 추가 되었습니까? –