JRadioButton class in java

JRadioButton class

This class is used to create a radio button. It is used to select one option from many options.

Constructors

  • JRadioButton()
  • JRadioButton(String s)
  • JRadioButton(String s, boolean selected)

Methods

  • public void setText(String s)
  • public String getText()
  • public void setEnabled(boolean b)
  • public void setIcon(Icon b)
  • public Icon getIcon()
  • public void addActionListener(ActionListener a):
Example
import javax.swing.*;
public class test
{
JFrame f;
test()
{
f=new JFrame();
JRadioButton r1=new JRadioButton(“Credit card”);
JRadioButton r2=new JRadioButton(“Debit Card”);
JRadioButton r3=new JRadioButton(“Net banking”);
r1.setBounds(50,100,100,30);
r2.setBounds(50,150,100,30);
r3.setBounds(50,200,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
f.add(r1);
f.add(r2);
f.add(r3);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args)
{
new test();
}
}

Output:

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *