我已经为性别制作了一个按钮组,但仍然启用了两个单选按钮。我尝试将性别群体换成年级。年级确实很好,而性别两个单选按钮都已启用。
这是我的代码:
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();
}
}
答案 0 :(得分:0)
我不确定你是否在这里粘贴了不完整的代码,因为我没有看到你的程序调用setGender()函数。由于单选按钮未分配给按钮组,因此可能会导致问题。
答案 1 :(得分:0)
如果你问为什么男性的 JRadioButton 都是女性,那么你就可以做到这一点。
ButtonGroup group = new ButtonGroup();
group.add(btnM);
group.add(btnF);
您需要对属于同一类别(组)的单选按钮进行分组。 将它们添加到组后,只会选择一个 JRadioButton 。