2016-06-22 6 views
1

私はJavaで調査をしようとしています。 20の質問と5つの選択肢があります。私は20ラベルと100ラジオボタンを5でグループ化することを考えていました。ラジオボタンが表示されません

Net BeanのGUIを使って手動でラベルを追加することができましたが、ボタンを生成したいのですが表示されません。ここに私のコードです:

public class Prikaz extends javax.swing.JFrame { 

    /** 
    * Creates new form Prikaz 
    */ 
    public Prikaz() { 
     initComponents(); 
     javax.swing.JRadioButton [][] buttons = new javax.swing.JRadioButton [20][5]; 
     ButtonGroup [] bg = new ButtonGroup[20]; 


     for (int i = 0; i < 20; ++i) { 
      bg[i]= new ButtonGroup(); 
      for (int j = 0; j < 5; ++j) { 
       buttons[i][j] = new javax.swing.JRadioButton(); 
       if(j==0)buttons[i][j].setSelected(true); 
       buttons[i][j].setLocation(1014+(j*24) , 44+(i*31)); 
       buttons[i][j].setToolTipText(((Integer)(j+1)).toString()); 
       buttons[i][j].setVisible(true); 
       buttons[i][j].setEnabled(true); 
       bg[i].add(buttons[i][j]); 
      } 
     } 

    } 

    // Variables declaration - do not modify      
    private javax.swing.JLabel jLabel1; 
    private javax.swing.JLabel jLabel10; 
    private javax.swing.JLabel jLabel11; 
    private javax.swing.JLabel jLabel12; 
    private javax.swing.JLabel jLabel13; 
    private javax.swing.JLabel jLabel14; 
    private javax.swing.JLabel jLabel15; 
    private javax.swing.JLabel jLabel16; 
    private javax.swing.JLabel jLabel17; 
    private javax.swing.JLabel jLabel18; 
    private javax.swing.JLabel jLabel19; 
    private javax.swing.JLabel jLabel2; 
    private javax.swing.JLabel jLabel20; 
    private javax.swing.JLabel jLabel21; 
    private javax.swing.JLabel jLabel3; 
    private javax.swing.JLabel jLabel4; 
    private javax.swing.JLabel jLabel5; 
    private javax.swing.JLabel jLabel6; 
    private javax.swing.JLabel jLabel7; 
    private javax.swing.JLabel jLabel8; 
    private javax.swing.JLabel jLabel9; 
    // End of variables declaration     
} 
+1

フォームやパネルにbuttongroupを追加する必要はありませんか? –

+0

新しいパネルとラベルに追加しようとしましたが、どちらの方法も表示されません – dreadnightmare

答えて

4

ButtonGroupに加えて、親コンポーネントにボタンを追加する必要があります。

jlabel1.add(buttons[i][j]);

編集:それは動作しない場合は、まず各JLabelためjLabel.setLayout(new FlowLayout())を行う 、その後、新しい追加

は、例えば、jlabel1にボタンを追加するには、ボタンを作成した後にこれを追加ボタン。

+0

まだ表示されません – dreadnightmare

+0

ラベルをフレームに追加しましたか? – johmsp

+0

私はあなたの 'setLocation()'メソッドについても懐疑的です。私は個人的に 'GridLayout'を使用します。 – johmsp

関連する問題