简体   繁体   中英

How do i display the radiobutton and label in the same row?

private String[] gender = {"Male","Female"};

private JComboBox jco = new JComboBox();

private JRadioButton[] jrbGender;
private ButtonGroup buttonGroup = new ButtonGroup();

private JButton jbtAdd = new JButton("Create");
private JButton jbtRetrieve = new JButton("Retrieve");
private JButton jbtUpdate = new JButton("Update");
private JButton jbtDelete = new JButton("Delete");

public RegistrationForMembership(){


    JPanel jp1 = new JPanel(new GridLayout(6,2));
    JPanel jp2 = new JPanel(new FlowLayout());
    JPanel jp3 = new JPanel(new GridLayout(1,1));

    jco = new JComboBox(membership);
    jrbGender = new JRadioButton[gender.length];

    add(jp1);

    jp1.add(new JLabel("Member ID"));
    jp1.add(jtfID);
    jp1.add(new JLabel("Member Name"));
    jp1.add(jtfName);
    jp1.add(new JLabel("Member IC"));
    jp1.add(jtfIC);
    jp1.add(new JLabel("Address"));
    jp1.add(jtfAddress);
    jp1.add(new JLabel("Gender"));

    for(int i =0; i<gender.length;++i){
        jrbGender[i] = new JRadioButton(gender[i]);
        buttonGroup.add(jrbGender[i]);
      jp1.add(jrbGender[i]);

    }

    add(jp1);

The one of the radio button will go to the next line , how do i let the radio button on the same line with the label ?

Add the JRadioButton s to a new JPanel and add that one to jp1 .

JPanel radios = new JPanel();
for (int i = 0; i < gender.length; ++i){
    jrbGender[i] = new JRadioButton(gender[i]);
    buttonGroup.add(jrbGender[i]);
    radios.add(jrbGender[i]);
}
jp1.add(radios);

Also, it looks like jp1 should have 5 rows, not 6.

JPanel jp1 = new JPanel(new GridLayout(5,2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM