我已經爲性別設置了一個按鈕組,但仍然啓用了兩個單選按鈕。我試着將這組性別交換到一年級。 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();
}
}
不確定你確實想要做什麼,但是你可以使用java.awt.Component.setEnabled(boolean)來啓用/禁用元素。 –
有關「myFirstGUI」的注意事項:如果您只是想了解Java中的GUI,請考慮JavaFX而不是Swing,因爲Swing處於維護模式,JavaFX是指定的繼承者。 – Puce
@Juriel看看我的解決方案,讓我知道它是否有幫助。 – user3437460