2015-07-10 193 views
0

在Eclipse中,我使用WindowBuilder開發GUI。我試圖動態創建按鈕和標籤並將其添加到面板,然後在按下另一個按鈕以顯示下一組按鈕/標籤時更新GUI。使用WindowBuilder動態創建JButton

我一直在嘗試幾個小時,只是不能得到它的工作。我有的代碼是成功生成前5個按鈕,但是當我點擊'下一步'(應該更新GUI的按鈕)時,它不起作用。但是,我正在使用sysout.println,並且可以看到我試圖更改的JLabel的實際文本值正在更改,它只是在GUI上不更新。 GUI最終將從數據庫中讀取數據並根據該數據填充標籤/按鈕,但最初我只是試圖讓它與手動創建的對象一起工作。

回答

1

這是我創造了我的動態一個JRadioButton:

  private void createJButton (int numOfBotons) 
     { 
      int x=20, y=300, width=40, height=50; //choose whatever you want 
      JRadioButton[] jRadioButton = new JRadioButton[numOfBotons]; 
      for(int i=0; i<numOfBotons; i++, y-=20) 
      { 
       jRadioButton[i] = new JRadioButton(""+i); 
       jRadioButton[i].setBounds(x, y, width, height); 
       group.add(jRadioButton[i]); 
       frame.add(jRadioButton[i]); 

      } 

     } 
+0

感謝我設法得到它現在的工作! –