62,628
社区成员
发帖
与我相关
我的任务
分享
//创建选择的选项
r1=new JRadioButton("老婆");
r2=new JRadioButton("情人");
r3=new JRadioButton("二奶");
//创建默认选项
r4=new JRadioButton("以上都不喜欢",true);
//创建ButtonGroup对象,不然就可以多选了,我们要的时单选
ButtonGroup bg=new ButtonGroup();
//在ButtonGroup中加入JRadioButton
bg.add(r1);
bg.add(r2);
bg.add(r3);
bg.add(r4);
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class test extends JFrame implements ActionListener{
private JPanel testJPanel;
private JRadioButton man;
private JRadioButton lady;
private ButtonGroup sex;
public void testframe(){
testJPanel=new JPanel();
//性别选项加入容器
man=new JRadioButton("男",true);
man.setBounds(150,250,50,20);
man.setBackground(Color.decode("#ffe7ba"));
lady=new JRadioButton("女");
lady.setBounds(200,250,50,20);
lady.setBackground(Color.decode("#ffe7ba"));
sex=new ButtonGroup();
sex.add(man);
sex.add(lady);
testJPanel.add(man);
testJPanel.add(lady);
this.add(testJPanel);
this.setSize(400,500);
this.setVisible(true);
}
public void actionPerformed(ActionEvent be){
}
public static void main(String args[]){
test a=new test();
a.testframe();
}
}