请尝试使用ButtonGroup组件并将两个名为male和female的JRadioButton组件添加到ButtonGroup对象中,然后使用setVisible(true)将其显示在JFrame中;方法.

下面的代码应该是有用的: –

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class Rb extends JFrame {
Rb (){
JRadioButton male = new JRadioButton("male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup bG = new ButtonGroup();
bG.add(male);
bG.add(female);
this.setSize(100,200);
this.setLayout( new FlowLayout());
this.add(male);
this.add(female);
male.setSelected(true);
this.setVisible(true);
}
public static void main(String args[]){
Rb j = new Rb();
}
}