本人要做一个好友信息记录,打算在(性别)这个属性中设计单选按钮,在性别(男,女)中只能选中一个,即是选中(男)按钮,就不会选中(女)按钮,否则,反之。请高手指点一下怎么实现这个功能?
解决方案 »
- JFrame如何实现小键盘输入?
- 今天老师让我用JAVA编点小游戏在我安卓的手机上试试,我有点迷茫。
- 为什么我执行XMLEncoder的close的时候会这么慢呢
- 13道基础题
- 小问题,在线等,解决马上给分!!
- 关于下载图像的问题
- 我下栽了borland enterprise application但是缺少序列号密码,谁给我一个,
- 怎样把 44,234,434 转换成 数字 44234434 ?? 就是说去掉逗号(寻求最简单的方法)
- 用JAVA跟其它编程语言结合
- catch的错误会引起程序崩溃吗?
- 关于泛型Collection<E>一个弱智问题.
- JRE 1.6.0_22 签名jar包里调用未签名jar包类异常
JRadioButton
使用ButtonGroup类即可:
ButtonGroup bGroup = new ButtonGroup();
bGroup.add(jrbMale);
bGroup.add(jrbFemale);
即可.注意:ButtonGroup并不是java.awt.component的子类,因此不能添加到容器中!
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();
}
}
//创建选择的选项
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);