简体   繁体   English

如何在同一行中显示单选按钮和标签?

[英]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 . JRadioButton添加到新的JPanel并将其添加到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. 另外,看起来jp1应该有5行,而不是6行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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