Java selection box, Radio box, and radio button _java

Source: Internet
Author: User

The selection box, the Radio box, and the radio button are all selection components, the selection component has two states, one is checked (on) and the other is unchecked (off), which provides a simple "On/off" selection feature that allows the user to choose among a set of selected items.

Selection box

The selection box (Jcheckbox) is checked or not open is a small box, is checked in the box tick. When you have more than one selection box in a container, and you can have multiple selection boxes selected, the selection box is also called a check box. The interface associated with the selection box is ItemListener, and the event class is itemevent.

The common construction methods of the Jcheckbox class are the following 3:

1.JCheckBox (): Constructs the selection box with an empty caption.
2.JCheckBox (String s): Constructs a selection box with the given title S.
3.JCheckBox (String s, Boolean B): Constructs the selection box with the given header s, and parameter B sets the initial state of the check or not.

Other common methods for Jcheckbox classes are as follows:

1.getState (): Gets the status of the selection box.
2.setState (Boolean B): Setting the status of the selection box
3.getLabel (): Gets the caption of the selection box.
4.setLabel (String s): Sets the caption of the selection box.
5.isSelected (): Gets the status of the selection box to be selected.
6.itemStateChanged (itemevent E): Interface method for handling selection box events.
7.getItemSelectable (): Gets the option to get the event source.
8.addItemListener (ItemListener L): Sets the monitor for the selection box.
9.removeItemListener (ItemListener L): Removes the monitor for the selection box.

Example 11-11 declares a panel subclass with 3 selection boxes for the panel child class object.

Class Panel1 extends jpanel{
Jcheckbox Box1,box2,box3;
Panel1 () {
Box1 = new Jcheckbox ("Football");
Box2 = new Jcheckbox ("Volleyball");
Box2 = new Jcheckbox ("basketball");
}
}

Single selection box

When you put more than one selection box in a container, and no Buttongroup object groups them, you can select more than one selection box at a time. If you use the Buttongroup object to group the selection boxes, multiple selection boxes in the same time group only allow one to be selected, saying that the selection box within the same group is a radio box. The way to Group a radio box is to create the Buttongroup object first, and then add the selection box that you want to the same group to the same Buttongroup object. See the Panel2 Declaration of the Panel subclass of the example 6.2 program, which has 3 radio boxes inside the group.

radio button

The radio button (Jradiobutton) function is similar to the one in the Radio box. The radio button is used to group some radio buttons with Buttongroup objects so that only one of the radio buttons in the same group is selected. The difference between a radio button and a radio box is that it is displayed in a different style, the radio button is a circular button, and the Radio box is a small box.

The common construction methods for the Jradiobutton class are as follows:
1.JRadioButton (): Constructs a radio button with an empty caption.
2.JRadioButton (String s): The radio button is constructed with the given title S.
3.JRadioButton (String S,boolean B): Constructs the radio button with the given header s, and parameter B sets the initial state of the check or not.

Radio buttons use Buttongroup to group radio buttons, and the radio buttons are grouped by creating objects and then adding the same group of radio buttons to the same Buttongroup object. See the Panel1 Declaration of the subclass of the example 6.2 program, which has 3 radio buttons in the group.

Select Project Event Handling

When a user chooses a selection box or radio button, the program responds to the selection by making the necessary response, and the program handles the Select item event. The basic elements of selecting a project handler are:

1. Monitor Select Project object class to implement Interface ItemListener,
2. Procedures to declare and establish a selection of objects,
3. Register the monitor for the Selection object,
4. Write an interface method itemstatechanged (ItemEvent e) that handles the selection of project events, in which the Getitemselectable () method is used to get the source of the event and handle it accordingly.

"Example 11-12" handles a small application that selects project events. A product selection group consisting of 3 radio buttons, and the text area displays information about the product when a product is selected. A number of purchased products, consisting of 3 selection boxes, displays each price in another text box when the purchase quantity is selected.

 Import java.applet.*;
 Import javax.swing.*;
 Import java.awt.*;
 Import java.awt.event.*;
   Class Panel1 extends jpanel{Jradiobutton box1,box2,box3;
   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,box3;
   Buttongroup G;
     Panel2 () {setlayout (new GridLayout (1,3));
     g = new Buttongroup ();
     Box1 = new Jcheckbox ("Buy 1 units");
     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);
   Add (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 fname[] = {"HP", "IBM", "DELL"};
   Static Double pritbl[][]={{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);
     Container con = This.getcontentpane ();
     Con.setlayout (New GridLayout (3,2));
     This.setlocation (100,100);
     This.setsize (400,100);
     Panel1 = new Panel1 ();p Anel2 = new 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);
     Panel1.box2.addItemListener (this);
     Panel1.box3.addItemListener (this);
     Panel2.box1.addItemListener (this);
     Panel2.box2.addItemListener (this);
     Panel2.box3.addItemListener (this); This.setvisible (TRUE);Pack (); The public void itemstatechanged (ItemEvent e) {//Option State has changed if (e.getitemselectable () ==panel1.box1) {//Get optional PROD
       uction = 0;
     Text1.settext (fname[0]+ "Company production"); Text2.settext ("");
       else if (e.getitemselectable () ==panel1.box2) {production = 1;
     Text1.settext (fname[1]+ "Company production"); Text2.settext ("");
       else if (e.getitemselectable () ==panel1.box3) {production = 2;
     Text1.settext (fname[2]+ "Company production"); Text2.settext ("");
       else{if (production ==-1) return;
       if (e.getitemselectable () ==panel2.box1) {Text2.settext ("" +pritbl[production][0]+ "million/Taiwan");
       else if (e.getitemselectable () ==panel2.box2) {Text2.settext ("" +pritbl[production][1]+ "million/Set");
       else if (e.getitemselectable () ==panel2.box3) {Text2.settext ("" +pritbl[production][2]+ "million/Set");
 The public class Example6_2 extends applet{mywindow mywin = new Mywindow ("Select Project Processing Sample program");

 }

The above is the entire contents of this article, I hope you can enjoy.

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.