2015-09-25 44 views
0

我已經爲性別設置了一個按鈕組,但仍然啓用了兩個單選按鈕。我試着將這組性別交換到一年級。 yearlevel確實很好,仍然是性別兩個單選按鈕被啓用。啓用了兩個選項的單選按鈕組

這裏是我的代碼:

public class myFirstGUI extends JFrame { 
    JPanel panel = new JPanel(); 
    JLabel lblName = new JLabel("Name:"); 
    JLabel lblyl = new JLabel("Year Level:"); 
    JLabel lblCourse = new JLabel("Course:"); 
    JLabel lblProg = new JLabel("Program:"); 
    JLabel lblGender = new JLabel("Gender:"); //Gender 
    JTextField txtName = new JTextField(); 
    JButton btnSubmit = new JButton("Submit"); 
    JButton btnReset = new JButton("Reset"); 
    GridBagLayout gLayout = new GridBagLayout(); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    JComboBox cboProgram = new JComboBox(); 
    JRadioButton rbtn1 = new JRadioButton("1st"); 
    JRadioButton rbtn2 = new JRadioButton("2nd"); 
    JRadioButton rbtn3 = new JRadioButton("3rd"); 
    JRadioButton rbtn4 = new JRadioButton("4th"); 
    JRadioButton rbtn5 = new JRadioButton("5th"); 
    JRadioButton rbtnM = new JRadioButton("Male"); //Gender Male 
    JRadioButton rbtnF = new JRadioButton("Female"); //Gender Female 
    JCheckBox chk003a = new JCheckBox("ITE003A"); 
    JCheckBox chk003 = new JCheckBox("CPE003"); 
    JCheckBox chk201 = new JCheckBox("CS201"); 
    ButtonGroup bg = new ButtonGroup(); 
    ButtonGroup bg1 = new ButtonGroup(); 

    public void setYearLevel() { 
     bg1.add(rbtn1); 
     bg1.add(rbtn2); 
     bg1.add(rbtn3); 
     bg1.add(rbtn4); 
     bg1.add(rbtn5); 
    } 

    public void setGender() { //Gender 
     bg.add(rbtnM); 
     bg.add(rbtnF); 
    } 

    public void setProgram() { 
     cboProgram.addItem("CPE"); 
     cboProgram.addItem("IE"); 
     cboProgram.addItem("ECE"); 
    } 

    public myFirstGUI() { 
     setYearLevel(); 
     setProgram(); 
     setSize(300, 500); 
     panel.setBackground(Color.CYAN); 
     setTitle("My First GUI"); 
     add(panel); 
     panel.setLayout(gLayout); 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.ipadx = 10; 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     panel.add(lblName, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     panel.add(txtName, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     panel.add(lblGender, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     panel.add(rbtnM, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 2; 
     panel.add(rbtnF, gbc); 

     rbtnM.setBackground(Color.cyan); 
     rbtnF.setBackground(Color.cyan); 

     gbc.gridx = 0; 
     gbc.gridy = 3; 
     panel.add(lblProg, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 3; 
     panel.add(cboProgram, gbc); 

     chk003a.setBackground(Color.cyan); 
     chk003.setBackground(Color.cyan); 
     chk201.setBackground(Color.cyan); 

     gbc.gridx = 0; 
     gbc.gridy = 4; 
     panel.add(lblCourse, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 4; 
     panel.add(chk003a, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 5; 
     panel.add(chk003, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 6; 
     panel.add(chk201, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 7; 
     panel.add(lblyl, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 7; 
     panel.add(rbtn1, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 8; 
     panel.add(rbtn2, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 9; 
     panel.add(rbtn3, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 10; 
     panel.add(rbtn4, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 11; 
     panel.add(rbtn5, gbc); 

     rbtn1.setBackground(Color.cyan); 
     rbtn2.setBackground(Color.cyan); 
     rbtn3.setBackground(Color.cyan); 
     rbtn4.setBackground(Color.cyan); 
     rbtn5.setBackground(Color.cyan); 

     gbc.gridx = 0; 
     gbc.gridy = 12; 
     panel.add(btnReset, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 12; 
     panel.add(btnSubmit, gbc); 

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     btnSubmit.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent e) { 
       JOptionPane.showMessageDialog(myFirstGUI.this, "Welcome " + txtName.getText() + 
        "\nProgram " + cboProgram.getSelectedItem() + 
        "\nYear Level: " + getYL()); 
      } 

     }); 

     btnReset.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       txtName.setText(""); 
       bg.clearSelection(); 
       bg1.clearSelection(); 
       cboProgram.setSelectedIndex(0); 
      } 
     }); 
    } 
    public String getYL() { 
     if (rbtn1.isSelected()) 
      return "1st"; 
     else if (rbtn2.isSelected()) 
      return "2nd"; 
     else if (rbtn3.isSelected()) 
      return "3rd"; 
     else if (rbtn4.isSelected()) 
      return "4th"; 
     else if (rbtn5.isSelected()) 
      return "5th"; 
     else 
      return "Please select your level"; 
    } 

    public static void main(String[] args) { 
     new myFirstGUI().show(); 
    } 
} 
+1

不確定你確實想要做什麼,但是你可以使用java.awt.Component.setEnabled(boolean)來啓用/禁用元素。 –

+0

有關「myFirstGUI」的注意事項:如果您只是想了解Java中的GUI,請考慮JavaFX而不是Swing,因爲Swing處於維護模式,JavaFX是指定的繼承者。 – Puce

+0

@Juriel看看我的解決方案,讓我知道它是否有幫助。 – user3437460

回答

0

我不知道,如果你已經爲我不要看到setGender()的調用函數從你的程序在這裏或不粘貼一個不完整的代碼。由於單選按鈕未分配給按鈕組,因此可能導致此問題。

+0

是的。沒有setGender()謝謝! – Juriel

0

如果你問爲什麼你的JRadioButton男性都是女性都選擇,這是你可以做的。

ButtonGroup group = new ButtonGroup(); 
group.add(btnM); 
group.add(btnF); 

您需要對屬於同一類別(組)的單選按鈕進行分組。 將它們添加到組後,只會選擇一個JRadioButton

+0

我做了兄弟。它被命名爲bg1。爲什麼都啓用是因爲我沒有setGender()。感謝您的解決方案:) – Juriel