我们使用JRadioButton类创建单选按钮。单选按钮用于从多个选项中选择一个选项。它用于填写表格、在线客观题和测验。
null
我们在按钮组中添加单选按钮,以便一次只能选择一个单选按钮。我们使用“ButtonGroup”类创建一个ButtonGroup,并在组中添加单选按钮。
使用的方法:
- JRadioButton():创建一个没有文本的未选中单选按钮。 例子:
JRadioButton j1 = new JRadioButton()
- JButton(String s):创建带有特定文本的JButton。 例子:
JButton b1 = new JButton("Button")
- JLabel(字符串s):创建带有特定文本的JLabel。 例子:
JLabel L = new JLabel("Label 1")
- ButtonGroup():用于创建一个组,我们可以在其中添加JRadioButton。在一个按钮组中,我们只能选择一个JRadioButton。 将单选按钮组合在一起的步骤。
- 使用“ButtonGroup()”方法创建ButtonGroup实例。
ButtonGroup G = new ButtonGroup()
- 现在,在“add()”方法的帮助下,在“G”组中添加按钮。
例子:
G.add(Button1); G.add(Button2);
- 使用“ButtonGroup()”方法创建ButtonGroup实例。
- isSelected():它将返回布尔值true或false,如果选择了JRadioButton,它将返回true,否则返回false。
例子:
JRadioButton.isSelected()
- Set(…)和Get(…)方法: i) Set和get用于替换直接从外部类访问成员变量。
ii)不是直接访问类成员变量,而是定义get方法来访问这些变量,并设置方法来修改它们。
本链接中给出了程序中使用的一些功能的说明: 功能描述
程序1:不带ActionListener的JRadioButton
// Java program to show JRadioButton Example. // in java. Importing different Package. import java.awt.*; import javax.swing.*; import java.awt.event.*; class Demo extends JFrame { // Declaration of object of JRadioButton class. JRadioButton jRadioButton1; // Declaration of object of JRadioButton class. JRadioButton jRadioButton2; // Declaration of object of JButton class. JButton jButton; // Declaration of object of ButtonGroup class. ButtonGroup G1; // Declaration of object of JLabel class. JLabel L1; // Constructor of Demo class. public Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JRadioButton" class. jRadioButton1 = new JRadioButton(); // Initialization of object of "JRadioButton" class. jRadioButton2 = new JRadioButton(); // Initialization of object of "JButton" class. jButton = new JButton( "Click" ); // Initialization of object of "ButtonGroup" class. G1 = new ButtonGroup(); // Initialization of object of " JLabel" class. L1 = new JLabel( "Qualification" ); // setText(...) function is used to set text of radio button. // Setting text of "jRadioButton2". jRadioButton1.setText( "Under-Graduate" ); // Setting text of "jRadioButton4". jRadioButton2.setText( "Graduate" ); // Setting Bounds of "jRadioButton2". jRadioButton1.setBounds( 120 , 30 , 120 , 50 ); // Setting Bounds of "jRadioButton4". jRadioButton2.setBounds( 250 , 30 , 80 , 50 ); // Setting Bounds of "jButton". jButton.setBounds( 125 , 90 , 80 , 30 ); // Setting Bounds of JLabel "L2". L1.setBounds( 20 , 30 , 150 , 50 ); // "this" keyword in java refers to current object. // Adding "jRadioButton2" on JFrame. this .add(jRadioButton1); // Adding "jRadioButton4" on JFrame. this .add(jRadioButton2); // Adding "jButton" on JFrame. this .add(jButton); // Adding JLabel "L2" on JFrame. this .add(L1); // Adding "jRadioButton1" and "jRadioButton3" in a Button Group "G2". G1.add(jRadioButton1); G1.add(jRadioButton2); } } class RadioButton { // Driver code. public static void main(String args[]) { // Creating object of demo class. Demo f = new Demo(); // Setting Bounds of JFrame. f.setBounds( 100 , 100 , 400 , 200 ); // Setting Title of frame. f.setTitle( "RadioButtons" ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
输出:
程序2:带有ActionListener的JRadioButton
// Java program to show JRadioButton Example. // in java. Importing different Package. import java.awt.*; import javax.swing.*; import java.awt.event.*; class Demo extends JFrame { // Declaration of object of JRadioButton class. JRadioButton jRadioButton1; // Declaration of object of JRadioButton class. JRadioButton jRadioButton2; // Declaration of object of JButton class. JButton jButton; // Declaration of object of ButtonGroup class. ButtonGroup G1; // Declaration of object of JLabel class. JLabel L1; // Constructor of Demo class. public Demo() { // Setting layout as null of JFrame. this .setLayout( null ); // Initialization of object of "JRadioButton" class. jRadioButton1 = new JRadioButton(); // Initialization of object of "JRadioButton" class. jRadioButton2 = new JRadioButton(); // Initialization of object of "JButton" class. jButton = new JButton( "Click" ); // Initialization of object of "ButtonGroup" class. G1 = new ButtonGroup(); // Initialization of object of " JLabel" class. L1 = new JLabel( "Qualification" ); // setText(...) function is used to set text of radio button. // Setting text of "jRadioButton2". jRadioButton1.setText( "Under-Graduate" ); // Setting text of "jRadioButton4". jRadioButton2.setText( "Graduate" ); // Setting Bounds of "jRadioButton2". jRadioButton1.setBounds( 120 , 30 , 120 , 50 ); // Setting Bounds of "jRadioButton4". jRadioButton2.setBounds( 250 , 30 , 80 , 50 ); // Setting Bounds of "jButton". jButton.setBounds( 125 , 90 , 80 , 30 ); // Setting Bounds of JLabel "L2". L1.setBounds( 20 , 30 , 150 , 50 ); // "this" keyword in java refers to current object. // Adding "jRadioButton2" on JFrame. this .add(jRadioButton1); // Adding "jRadioButton4" on JFrame. this .add(jRadioButton2); // Adding "jButton" on JFrame. this .add(jButton); // Adding JLabel "L2" on JFrame. this .add(L1); // Adding "jRadioButton1" and "jRadioButton3" in a Button Group "G2". G1.add(jRadioButton1); G1.add(jRadioButton2); // Adding Listener to JButton. jButton.addActionListener( new ActionListener() { // Anonymous class. public void actionPerformed(ActionEvent e) { // Override Method // Declaration of String class Objects. String qual = " " ; // If condition to check if jRadioButton2 is selected. if (jRadioButton1.isSelected()) { qual = "Under-Graduate" ; } else if (jRadioButton2.isSelected()) { qual = "Graduate" ; } else { qual = "NO Button selected" ; } // MessageDialog to show information selected radion buttons. JOptionPane.showMessageDialog(Demo. this , qual); } }); } } class RadioButton { // Driver code. public static void main(String args[]) { // Creating object of demo class. Demo f = new Demo(); // Setting Bounds of JFrame. f.setBounds( 100 , 100 , 400 , 200 ); // Setting Title of frame. f.setTitle( "RadioButtons" ); // Setting Visible status of frame as true. f.setVisible( true ); } } |
输出:
按下“点击”按钮后。
Program 3程序创建一组简单的单选按钮(带有图像),并向其添加项目侦听器
// Java Program to create a simple group of radio buttons // (with image )and add item listener to them import java.awt.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame implements ItemListener { // frame static JFrame f; // radiobuttons static JRadioButton b, b1; // create a label static JLabel l1; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object solve s = new solve(); // create a panel JPanel p = new JPanel(); // create a new label JLabel l = new JLabel( "which website do you like?" ); l1 = new JLabel( "geeksforgeeks selected" ); // create Radio buttons b = new JRadioButton( "geeksforgeeks" , new ImageIcon( "f:/gfg.jpg" )); b1 = new JRadioButton( "others" ); // create a button group ButtonGroup bg = new ButtonGroup(); // add item listener b.addItemListener(s); b1.addItemListener(s); // add radio buttons to button group bg.add(b); bg.add(b1); b.setSelected( true ); // add button and label to panel p.add(l); p.add(b); p.add(b1); p.add(l1); f.add(p); // set the size of frame f.setSize( 400 , 400 ); f.show(); } public void itemStateChanged(ItemEvent e) { if (e.getSource() == b) { if (e.getStateChange() == 1 ) { l1.setText( "geeksforgeeks selected" ); } } else { if (e.getStateChange() == 1 ) { l1.setText( "others selected" ); } } } } |
输出:
注意:以下程序可能无法在联机编译器中运行,请使用脱机IDE。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END