Select boxes, Radio boxes, and radio buttons are select components, select components in two states, one is checked (on), the other is unchecked (off), and they provide a simple "on/off" selection feature that allows the user to choose between a set of selection items.
Selection boxSelection box (jcheckbox) is selected or not open is a small box, is selected in the boxes tick. A selection box is also called a check box when there are multiple selection boxes in a container, and multiple selection boxes can be selected. The interface associated with the selection box is ItemListener, and the event class is itemevent.
The Jcheckbox class is commonly used for the following 3 construction methods:
Other common methods of the Jcheckbox class are as follows:
Example 11-11 declares a panel subclass, and the Panel subclass object has 3 selection boxes.
Extends jpanel{ jcheckbox box1,box2,box3; Panel1 () { new Jcheckbox ("football"); newnew Jcheckbox ("basketball");}}
Radio Box
When multiple selection boxes are placed in a container and no Buttongroup objects are grouped, you can select multiple selection boxes at the same time. If you use the Buttongroup object to group the selection boxes, multiple selection boxes within the same time group allow only one to be selected, and the selection box within the same group is called a radio box. The way to Group a radio box is to create a Buttongroup object first, and then add the selection boxes that you want to the same group to the same Buttongroup object. See example 6.2 of the Panel subclass Panel2 Declaration of the program, with 3 radio boxes within the group.
radio buttonThe radio button (Jradiobutton) functions like a radio box. The radio buttons are used to group some radio buttons with Buttongroup objects so that only one of the radio buttons in the same group is allowed to be selected. The difference between a radio button and a radio box is that the style displayed is different, the radio button is a circular button, and a radio box is a small square.
The common construction methods of the Jradiobutton class are as follows:
Radio buttons are used to group radio buttons using Buttongroup, and radio buttons are grouped by first creating objects and then adding radio buttons of the same group to the same Buttongroup object. See the declaration of subclass Panel1 of Example 6.2 program, with 3 radio buttons in the group.
Select Project Event HandlingAfter the user chooses the selection box or radio button, the program responds to the selection, and the program handles the selection of project events. The basic contents of selecting a project handler are:
Example 11-12 deals with small applications that select project events. A product selection group consisting of 3 radio buttons that, when selected, displays information about the product in the text area. A selection box Group of 3 selection boxes that, when purchased quantity is selected, displays each price in another text box.
1Import java.applet.*;2Import javax.swing.*;3Import java.awt.*;4Import java.awt.event.*;5Class Panel1Extendsjpanel{6Jradiobutton Box1,box2,box3;7Buttongroup G;8Panel1 () {9 SetLayout (New GridLayout (1,3));Ten g =NewButtongroup ();One box1 =New Jradiobutton (mywindow.fname[0]+ "Computer",False);Box2 =New Jradiobutton (mywindow.fname[1]+ "Computer",False);Box3 =New Jradiobutton (mywindow.fname[2]+ "Computer",False);14G.add (Box1); G.add (Box2); G.add (BOX3);15Add (box1); add (box2); add (BOX3);Add (New JLabel ("Computer 3 selected 1") );17}18}19Class Panel2Extendsjpanel{20Jcheckbox Box1,box2,box3;21stButtongroup G;22Panel2 () {SetLayout (New GridLayout (1,3));g =NewButtongroup ();Box1 =New Jcheckbox ("Buy 1 units");Box2 =New Jcheckbox ("Buy 2 units");Box3 =New Jcheckbox ("Buy 3 units");28G.add (Box1); G.add (Box2); G.add (BOX3);29Add (box1); add (box2); add (BOX3);Add (New JLabel ("Select 1, 2, or 3"));31}32}33Class MywindowExtends JFrameImplementsitemlistener{34Panel1 Panel1;35Panel2 Panel2;36JLabel Label1,label2;37JTextArea text1,text2;38Static String fname[] = {"HP", "IBM", "DELL"};39StaticDouble pritbl[][]={{1.20,1.15,1.10},{1.70,1.65,1.60},{1.65,1.60,1.58}};40Staticint productin =-1;41Mywindow (String s) {42Super(s);Container con =This. Getcontentpane ();Con.setlayout (New GridLayout (3,2));45This.setlocation (100,100);46This.setsize (400,100);Panel1 =New Panel1 ();p Anel2 =NewPanel2 ();Label1 =New JLabel ("Product Introduction", Jlabel.center);Label2 =New JLabel ("Product price", Jlabel.center);Text1 =New JTextArea (); text2 =NewJTextArea ();51Con.add (Label1); Con.add (Label2); Con.add (Panel1);52Con.add (Panel2); Con.add (Text1); Con.add (TEXT2);Panel1.box1.addItemListener (This);Panel1.box2.addItemListener (This);Panel1.box3.addItemListener (This);Panel2.box1.addItemListener (This);Panel2.box2.addItemListener (This);Panel2.box3.addItemListener (This);59This.setvisible (true);This. Pack ();60}61Publicvoid itemstatechanged (ItemEvent e) {//Option State has changed62if (e.getitemselectable () ==panel1.box1) {//Get optional optionsProduction =0;Text1.settext (fname[0]+ "Company production"); Text2.settext ("");65}66Elseif (e.getitemselectable () = =Panel1.box2) {Production =1;Text1.settext (fname[1]+ "Company production"); Text2.settext ("");69}70Elseif (e.getitemselectable () = =PANEL1.BOX3) {Production =2;Text1.settext (fname[2]+ "Company production"); Text2.settext ("");73}74Else{75if (production ==-1)Return;76if (e.getitemselectable () = =Panel2.box1) {Text2.settext ("+pritbl[production][0]+" million yuan/Taiwan ");78}79Elseif (e.getitemselectable () = =Panel2.box2) {Text2.settext ("+pritbl[production][1]+" million yuan/Taiwan ");81}82Elseif (E.getitemselectable () ==panel2.box3) {83 text2.settext ("+pritbl[production][2]+" million/Taiwan "}85 }86 }87 }88 public class Example6_2 extends Applet{89 Mywindow Mywin = new Mywindow ("Select Project Handling Sample Program"