2013-10-10 52 views
1

我正在使用NetBeans IDE來開發一個Swing應用程序,並且在一個JDialog窗口中,我試圖使用一些由netbeans添加的組件,還有一些組件動態地添加了組件,但是當我嘗試將組件添加到未顯示代碼中的initComponents。Netbeans initComponents正在竊聽代碼,該如何解決?

代碼:

public RegrasUsuarioDialog(java.awt.Frame parent, boolean modal) { 
    super(parent, "Preferências de conversão", modal); 
    //initComponents(); 
    JRadioButton radioButton = new JRadioButton("Radio Button"); 

    optionsPanel = new JPanel(new GridLayout(0, 1)); 
    add(optionsPanel, BorderLayout.LINE_START); 

    optionsPanel.add(radioButton); 



} 

當我取消了的initComponents()方法,並發表意見:

optionsPanel = new JPanel(new GridLayout(0, 1)); 
    add(optionsPanel, BorderLayout.LINE_START); 

讓Netbeans的代碼創建它不工作的基本組件。

+0

是使用'GroupLayout'的NetBeans? – MadProgrammer

+0

閱讀並瞭解Java佈局管理器及其工作原理。這將幫助您學習如何在運行時向組件添加容器。 –

+0

是的,NetBeans使用GroupLayout,我真的認爲這就是爲什麼我無法添加新組件,我將研究如何使用佈局管理器添加組件。謝謝。 –

回答

0

您不能重新分配optionsPanel變量,因爲initComponents()正在初始化它。如果要自行初始化面板,請在設計視圖中選擇面板,然後在「屬性」窗口中選擇「代碼」選項卡,並將相同的自定義初始化代碼放入自定義創建代碼區域。見下面的截圖:

Properties screen shot

0

獲取容器中,然後添加組件,它就會加入到表單

Container layout = getContentPane(); 
layout.add(button1); 
相關問題