[英]Radio buttons don't show
我正在嘗試用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
}
除了ButtonGroup外,還需要將按鈕添加到父組件中。
例如,要將按鈕添加到jlabel1
,請在創建按鈕后添加此按鈕:
jlabel1.add(buttons[i][j]);
編輯:如果不起作用,請首先為每個JLabel
執行jLabel.setLayout(new FlowLayout())
,然后添加新按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.