The single-choice button is to specify one of Multiple display information. In swing, you can use JRadioButton to complete the operation of a group of single-choice buttons.
Package com. beyole. util; import java. awt. container; import java. awt. gridLayout; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import javax. swing. borderFactory; import javax. swing. buttonGroup; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. JRadioButton; class MyRadio {private JFrame jFrame = new JFrame ("Beyole"); // defines a form private Container container = jFrame. getContentPane (); // get the form container private JRadioButton jb1 = new JRadioButton (""); // define a single choice button private JRadioButton jb2 = new JRadioButton (" "); // define a single-choice button private JRadioButton jb3 = new JRadioButton ("Forum"); // define a single-choice button private JPanel panel = new JPanel (); /// define a panel public MyRadio () {panel. setBorder (BorderFactory. createTitledBorder ("select your favorite website"); // defines a panel's border display panel. setLayout (new GridLayout (1, 3); // defines the layout, one row and three columns of panel. add (this. jb1); // Add the component panel. add (this. jb2); // Add the component panel. add (this. jb3); // Add the component ButtonGroup group = new ButtonGroup (); group. add (this. jb1); group. add (this. jb2); group. add (this. jb3); container. add (panel); // add the panel this. jFrame. setSize (330, 80); // set the form size this. jFrame. setVisible (true); // display the form this. jFrame. addWindowListener (new WindowAdapter () {// Add event listening public void windowClosing (invalid wevent arg0) {// method of closing the rewrite window System. exit (1); // system exit}) ;}public class JRadioButtonDemo {public static void main (String [] args) {new MyRadio ();}}
One of the code here is worth noting. Without this code, single-choice buttons are not really single-choice buttons. At that time, multiple choice is supported.
ButtonGroup group = new ButtonGroup();group.add(this.jb1);group.add(this.jb2);group.add(this.jb3);
Here, we create a group for the single-choice button. At this time, there will be no multiple choices.
Program: