📜  JRadioButton | Java摇摆

📅  最后修改于: 2022-05-13 01:54:30.230000             🧑  作者: Mango

JRadioButton | Java摇摆

我们使用 JRadioButton 类来创建一个单选按钮。单选按钮用于从多个选项中选择一个选项。它用于填写表格、在线目标论文和测验。

我们在 ButtonGroup 中添加单选按钮,以便我们一次只能选择一个单选按钮。我们使用“ButtonGroup”类来创建一个 ButtonGroup 并在组中添加单选按钮。

使用方法:

  1. JRadioButton() :创建一个没有文本的未选中的 RadioButton。
    例子:
    JRadioButton j1 = new JRadioButton() 
    
  2. JButton(String s) : 创建一个带有特定文本的 JButton。
    例子:
    JButton b1 = new JButton("Button") 
    
  3. JLabel(String s) : 创建一个带有特定文本的 JLabel。
    例子:
    JLabel L = new JLabel("Label 1") 
    
  4. ButtonGroup() :用于创建一个组,我们可以在其中添加 JRadioButton。我们只能在 ButtonGroup 中选择一个 JRadioButton。
    将单选按钮组合在一起的步骤。
    • 使用“ButtonGroup()”方法创建一个 ButtonGroup 实例。
      ButtonGroup G = new ButtonGroup()
      
    • 现在借助“add()”方法在组“G”中添加按钮。

      例子:

      G.add(Button1);
      G.add(Button2);
      
  5. isSelected() :它将返回一个布尔值 true 或 false,如果选择了 JRadioButton,它将返回 true,否则返回 false。

    例子:

    JRadioButton.isSelected()
    
  6. 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);
    }
}

输出:

按下按钮“点击”后。

程序 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。