The program can use JRadioButton to implement the single-choice button function. To implement the check box function, JCheckBox must be used.
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. JCheckBox; import javax. swing. JFrame; import javax. swing. JPanel; class MyCheckBox {private JFrame frame = new JFrame ("Beyole"); // defines the form private Container container = frame. getContentPane (); // get the form container private JCheckBox jcb1 = new JCheckBox (""); // define a check box private JCheckBox jcb2 = new JCheckBox (" "); // define a check box private JCheckBox jcb3 = new JCheckBox ("blog"); // define a check box private JPanel = new JPanel (); // define a panel public MyCheckBox () {panel. setBorder (BorderFactory. createTitledBorder ("select your favorite website"); // defines a panel's border display panel. setLayout (new GridLayout (1, 3); // defines the layout, with one row and three columns of panel. add (this. jcb1); // Add the component panel. add (this. jcb2); // Add the component panel. add (this. jcb3); // Add the component container. add (panel); // add the panel this. frame. setSize (330, 80); // defines the size of the form. this. frame. setVisible (true); // display the form this. frame. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent arg0) // Method for closing the rewrite window {System. exit (1); // exit});} public class JCheckBoxDemo01 {public static void main (String [] args) {new MyCheckBox ();}}
It can be found that, unlike the single-choice button, the selected area is changed to a box.
Program: