如何在核心java程序中添加单选按钮组,以便一次只选择一个单选按钮? [英] How to add a radio button group in a core java program such that only one radio button is selected at one time?

查看:197
本文介绍了如何在核心java程序中添加单选按钮组,以便一次只选择一个单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在核心java中构建一个项目。我坚持制作一个单选按钮组(用于输入性别(男/女)。为此我需要一个无线电组,以便一次只选择一个单选按钮;并相应地将输入输入数据库。请帮忙。

I am building a project in core java. BUt i'm stuck in making a radio button group ( for entering the gender (male/female). For that i need a radio group such that only one radio button is selected at one time; and take the input into the database accordingly. Please help.

推荐答案

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

Kindly try using ButtonGroup component and add two JRadioButton components named male and female to the ButtonGroup object and then display it in a JFrame using setVisible(true); method.

以下代码应该有用: -

The Below code should be useful :-

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();
}

}

这篇关于如何在核心java程序中添加单选按钮组,以便一次只选择一个单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆