Java know how much (87) Select boxes and radio buttons

Source: Internet
Author: User

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 box

Selection 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:

    1. Jcheckbox (): Constructs a selection box with an empty caption.
    2. Jcheckbox (String s): Constructs a selection box with the given caption s.
    3. Jcheckbox (String s, Boolean B): Constructs a selection box with the given caption s, and parameter B sets the initial state of whether or not it is selected.


Other common methods of the Jcheckbox class are as follows:

    1. GetState (): Gets the status of the selection box.
    2. SetState (Boolean B): Sets the state of the selection box
    3. Getlabel (): Gets the title of the selection box.
    4. SetLabel (String s): Sets the title of the selection box.
    5. IsSelected (): Gets the status of whether the selection box is selected.
    6. Itemstatechanged (ItemEvent E): The interface method that handles the selection box event.
    7. Getitemselectable (): Gets an option to get the event source.
    8. Additemlistener (ItemListener L): Sets the monitor for the selection box.
    9. Removeitemlistener (ItemListener L): Removes the monitor from the selection box.


Example 11-11 declares a panel subclass, and the Panel subclass object has 3 selection boxes.

class extends jpanel{    jcheckbox box1,box2,box3;    Panel1 () {        new  Jcheckbox ("football");         New Jcheckbox ("Volleyball");         New 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 button

The 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:

    1. Jradiobutton (): Constructs a radio button with an empty caption.
    2. Jradiobutton (String s): Constructs a radio button with the given caption s.
    3. Jradiobutton (String S,boolean B): Constructs a radio button with the given caption s, and parameter B sets the initial state of the selection or not.

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 Handling

After 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:

    1. Monitor the class of the selected project object to implement the interface ItemListener,
    2. Program to declare and establish the Selection object,
    3. To register the monitor for the Select object,
    4. Write the interface method itemstatechanged (ItemEvent e) that handles the selection of project events, and use the Getitemselectable () method to obtain the event source and handle it accordingly.


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.

1 Importjava.applet.*;2 Importjavax.swing.*;3 Importjava.awt.*;4 Importjava.awt.event.*;5 classPanel1extendsjpanel{6 Jradiobutton Box1,box2,box3;7 Buttongroup G;8 Panel1 () {9SetLayout (NewGridLayout (1,3));Teng =NewButtongroup (); OneBox1 =NewJradiobutton (mywindow.fname[0]+ "Computer",false); ABox2 =NewJradiobutton (mywindow.fname[1]+ "Computer",false); -Box3 =NewJradiobutton (mywindow.fname[2]+ "Computer",false); - G.add (box1); G.add (Box2); G.add (Box3) ; the Add (box1); Add ( box2); Add (Box3) ; -AddNewJLabel ("Computer 3 selected 1") ); -     } - } + classPanel2extendsjpanel{ - Jcheckbox Box1,box2,box3; + Buttongroup G; A Panel2 () { atSetLayout (NewGridLayout (1,3)); -g =NewButtongroup (); -Box1 =NewJcheckbox ("Buy 1 units"); -Box2 =NewJcheckbox ("Buy 2 units"); -Box3 =NewJcheckbox ("Buy 3 units"); - G.add (box1); G.add (Box2); G.add (Box3) ; in Add (box1); Add ( box2); Add (Box3) ; -AddNewJLabel ("Select 1, 2, or 3")); to     } + } - classMywindowextendsJFrameImplementsitemlistener{ the Panel1 Panel1; * Panel2 Panel2; $ JLabel Label1,label2;Panax Notoginseng JTextArea text1,text2; -     StaticString fname[] = {"HP", "IBM", "DELL"}; the     Static Doublepritbl[][]={{1.20,1.15,1.10},{1.70,1.65,1.60},{1.65,1.60,1.58}}; +     Static intProductin =-1; A Mywindow (String s) { the         Super(s); +Container con = This. Getcontentpane (); -Con.setlayout (NewGridLayout (3,2)); $          This. setlocation (100,100); $          This. SetSize (400,100); -Panel1 =NewPanel1 ();p Anel2 =NewPanel2 (); -Label1 =NewJLabel ("Product Introduction", jlabel.center); theLabel2 =NewJLabel ("Product price", jlabel.center); -Text1 =NewJTextArea (); text2 =NewJTextArea ();Wuyi Con.add (Label1); Con.add (Label2); Con.add (Panel1) ; the Con.add (Panel2); Con.add (Text1); Con.add (Text2) ; -Panel1.box1.addItemListener ( This); WuPanel1.box2.addItemListener ( This); -Panel1.box3.addItemListener ( This); AboutPanel2.box1.addItemListener ( This); $Panel2.box2.addItemListener ( This); -Panel2.box3.addItemListener ( This); -          This. setvisible (true); This. Pack (); -     } A      Public voidItemstatechanged (ItemEvent e) {//option State has changed +         if(E.getitemselectable () ==panel1.box1) {//Get optional Options theProduction =0; -Text1.settext (fname[0]+ "Company production"); Text2.settext (""); $         } the         Else if(e.getitemselectable () = =Panel1.box2) { theProduction =1; theText1.settext (fname[1]+ "Company production"); Text2.settext (""); the         } -         Else if(e.getitemselectable () = =panel1.box3) { inProduction =2; theText1.settext (fname[2]+ "Company production"); Text2.settext (""); the         } About         Else{ the             if(Production ==-1)return; the             if(e.getitemselectable () = =panel2.box1) { theText2.settext ("+pritbl[production][0]+" million yuan/Taiwan "); +             } -             Else if(e.getitemselectable () = =Panel2.box2) { theText2.settext ("+pritbl[production][1]+" million yuan/Taiwan ");Bayi             } the             Else if(e.getitemselectable () = =panel2.box3) { theText2.settext ("+pritbl[production][2]+" million yuan/Taiwan "); -             } -         } the     } the } the  Public classExample6_2extendsapplet{ theMywindow Mywin =NewMywindow ("Select Project Handling Sample Program"); -}

Series Articles:

Java know how much (top)Java know how much (medium)Java knows how many () Java vectors (vector) and their applicationsJava know how much (79) hash table and its applicationJava know how much (80) graphical Interface design basicsJava know how much (81) frame window BasicsJava know how much (82) Introduction to tags, buttons, and button eventsJava know how much (83) Panel Basics: JPanel and JScrollPaneJava know how much (84) layout design of graphical interfaceJava know how much (85) text box and text areaJava know how much (86) input and output of text box and text area

Java know how much (87) Select boxes and radio buttons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.