Java Program to create JRadioButton from text

Java 8Object Oriented ProgrammingProgramming

The following is an example to create JRadioButton from text −

Example

package my;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
public class SwingDemo {
   public static void main(String[] args) {
      JRadioButton radio1 = new JRadioButton("Cricket");
      JRadioButton radio2 = new JRadioButton("Football");
      ButtonGroup group = new ButtonGroup();
      group.add(radio1);
      group.add(radio2);
      radio1.setSelected(true);
      JFrame frame = new JFrame();
      frame.setLayout(new FlowLayout());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(new JLabel("Fav Sports:"));
      frame.add(radio1);
      frame.add(radio2);
      frame.pack();
      frame.setVisible(true);
   }
}

Output

raja
Published on 08-May-2019 12:44:18
Advertisements