The selection box, single -selection box, and single -selection button are selected components. There are two states for choosing components, one is selected (on), and the other is unatient to be selected. They provide a simple "ON/OFF "The selection function allows users to choose in a group of selection projects.
Choice box
The selection of the selection box (jcheckbox) is a small box, which is selected in the box. When there are multiple selection boxes in one container, and multiple selection boxes can be selected at the same time, such a selection box is also called the check box. The interface related to the selection box is Itemlistener, and the event class is Itemvent.
The common constructed methods commonly used in the JCHECKBOX class include the following 3:
1.jcheckbox (): Construct a selection box with a empty title.
2.jcheckbox (String S): Use a given title s constructor selection box.
3.jcheckbox (String S, Boolean B): Use a given title s constructor selection box, parameter B set the initial state of selection or not.
Other common methods of the JCHECKBOX class are as follows:
1.getState (): Get the state of the selection box.
2.SetState (Boolean B): Set the status of the selection box
3.Getlabel (): Get the title of the selection box.
4.Setlabel (String S): Set the title of the selection box.
5.isselect (): Whether the selected box is selected.
6.itemStateChanged (ITemevent E): processing the interface method of the selection box event.
7.GetITEMSELECTable (): Get the options and get the source of the event.
8.Additemlistener (itemlistener L): Set a monitor for the selection box.
9.RmoveItemListener (Itemlistener L): Monitor of the selection box.
[Example 11-11] State a panel class, and there are three options boxes.
Class Panel1 Extends Jpanel {
Jcheckbox box1, box2, box3;
Panel1 () {{
box1 = new jcheckbox ("football");
box2 = new jcheckbox ("volleyball");
box2 = new jcheckbox ("basketball");
}
}
Single -choice
When multiple selection boxes are placed in a container, and without the Buttonidroup object, they can be selected at the same time. If you use the Buttongroup object, the selection box will be selected. At the same time, multiple selection boxes in the group are allowed to be selected. The selection box in the same group is a single selection box. The method of grouping box grouping is to first create the Buttongroup object, and then add the selection box to the same group to the same Buttongroup object. See the statement of Panel2 of Panel2, a panel class 6.2 program 6.2.
Single -choice button
The function of the single -selection button (Jradiobutton) is similar to a single selection box. The method of using the single -selection button is to group some radio buttons with the Buttongroup object, so that the same group of radio buttons allow only one to be selected. The difference between the single -selection button and the single -election box is different styles. The single -selection button is a circular buttons, and the single -selection box is a small box.
The common constructors of the JRADIOBUTTON class are the following:
1.jradiobutton (): Construct a single -selection button with an empty title.
2.jradiobutton (String S): Use a given title S to construct a radio button.
3.jradiobutton (String S, Boolean B): Use a given title S constructive button, parameter B set the initial state of selection or not.
When using the single -selection button, you need to use buttongroup to group the single -selected button. The grouping method of the single -selection button is to create an object first, and then add the single -selection button in the same group to the same Buttongroup object. See the statement of Panel1 of the sub -class 6.2 program 6.2, and there are 3 radical buttons in the group.
Select project event processing
After the user makes the selection box or a radio button, the program should respond to this choice. The program should handle the selection project event. The basic content of the selection of the project processing program is:
1. Surveillance selection of the class of the project object to realize the interface itemlistener,
2. The program should declare and establish a selection object,
3. In order to select the object registration monitor,
4. Write the interface method of the selection of the project events ItemStateChanged (Itemevent E). In this method, use the getItemSelectable () method to obtain the source of the event and make corresponding processing.
[Example 11-12] Treatment of small applications for selecting project events. A product selection group consisting of three single -selected buttons. When a product is elected, the text area will display the information of the product. One choice box group consisting of 3 selection boxes. When the number of purchases is selected, each price is displayed in another text box.
Import java.applet.*; Import javax.swing.*; Import java.awt.*; Import java.awt.event.*; Class Panel1 Extends Jpanel {Jradiobutton Box1, Box2, B Ox3; Buttongroup g; Panel1 () {setlayout (New GridLayout (1,3)); g = new buttongroup (); box1 = new jradiobutton (mywindow.fname [0]+"computer", false); box2 = new jradiobutton (mywindow. fname [1]+"computer ", False); box3 = new jradiobutton (mywindow.fname [2]+" computer ", false); g.add (box1); g.add (box2); g.add (box3); add (box1); add (box2); add (box3); add (new jlabel ("Computer 3 select 1"));} Class Panel2 Extends jpanel {jcheckbox box1, box2, buttongroup g; Panel2 () {setlay out (New GridLayout ( 1,3)); g = new buttongroup (); box1 = new jcheckbox ("buy 1"); box2 = new jcheckbox ("buy 2 units"); box3 = new jcheckbox ("buy 3 units"); g .add (box1); g.add (box2); g.add (box3); add (box1); add (box2); add (box3); addd (new jlabel ("select 1, 2 or 3") ;} Class Mywindow Extends Jframe Implements itemlistener {Panel1 Panel1; Panel2 Panel2; JLABEL LABEL1, Label2; JTEXTAREA Text1, Text2; Static String [ ] = {"Hp", "IBM", "Dell"}; Static double propbl [ ] [] = {{1.20, 1.15,1.10}, {1.70, 1.65,1.60}, {1.65, 1.60,1.58}; static int Productin = -1; mywindow (string s) {super (s); = this.GetContentPane (); con.setLayout (New GridLayout (3,2)); this.SetLocation (100,100); this.Setsize (400,100); Panel1 = New Panel1 (); Panel2 = n ew panel2 (); label1 = New Jlabel ("Product Introduction", JLABEL.CENTER); Label2 = New Jlabel ("Product Price", JLABEL.Center); Text1 = New Jtextarea (); Text2 = new jtextarea (); con.add (label1); con .add (label2); con.add (Panel1); con.add (Panel2); con.add (text1); con.add (text2); Panel1.BOX1.Additemlistener (this); this );; Panel2.BOX1.Additemlistener (this); Panel2.BOX2.AddItemlistener (this); Panel2.BOX3.Additemlistener (this); His.setvisible (true); this.pack () ;} Public void itemStateChanged (itemevent E) {// option status has changed if (e.getITEMSELECTable () == Panel1.box1) {// Get the optional product = 0; text1.setText (FNA me [0]+"company Production "); Text2.setText (" ");} Else if (e.getItemselectable () == Panel1.box2) {Production = 1; Text1.setText (FNAME [1]+" Company Production "); Text2.Settex T ("");} Else if (e.getItemselectable () == Panel1.box3) {Production = 2; Text1.setText (FNAME [2]+"Company Production"); Text2.Settext (""); {if (problem ==-1) return; if (e.getItemselectable () == Panel2.box1) {text2.setText (""+ProtBl [0]+"10] 10,000 yuan");} else If (E.getITEMSELECTable () == Panel2.Box2) {text2.setText (""+ProtBl [Production] [1]+"10,000 yuan");} Else if (e.GetiteMelectable () == Panel2. Box3) {text2.Settext (""+Pritbl [Production] [2]+"10,000 yuan/Taiwan");}}} public class exmple6_2 Extends Applet {Mywindow Mywin = NewIndow Treatment sample program ") ;}The above is all the contents of this article. I hope everyone can like it.