radio buttons and check boxes

Source: Internet
Author: User

Jradiobutton Constructor:
Jradiobutton (): Establish a new Jradiobutton.
Jradiobutton (icon icon): Create a Jradiobutton with images but no text.
Jradiobutton (Icon Icon,boolean selected): Creates a Jradiobutton with an image but no text, and sets its initial state (with or without selection).
Jradiobutton (String text): Create a Jradiobutton with text.
Jradiobutton (String Text,boolean selected): Creates a Jradiobutton with text and sets its initial state (with or without selection).
Jradiobutton (String Text,icon Icon): Creates a Jradiobutton with text and images with no initial state selected.

Jradiobutton (String text,icon Icon,boolean selected): Creates a text-based Jradiobutton with an image and sets its initial state (with or without selection)

To change the RadioButton to a single selection, we must use the Buttongroup class. This class is located under the package javax.swing, the main function of the Buttongroup class is that only one component will have the status "on" at the same time, and the other is "off", that is, only one component will be selected at the same time. While the Buttongroup class can be used by AbstractButton the following subclasses, the most commonly used are Jradiobutton, Jradiobuttonmenu, item, and Jtogglebutton components

The Buttongroup class is constructed as follows:
Buttongroup () Create a new Buttongroup ()
The common methods of the Buttongroup () class are as follows:
public void Add (AbstractButton B): Add a button to the group
public void ClearSelection (): Clears the selection, i.e. no buttons in the group of buttons are selected
Pubic int Getbuttoncount (): Gets the number of buttons in this group
Public enumeration<abstractbutton>getelemeent (): Gets the buttons used in this group
public void Remove (AbstractButton b): Remove button from button

Jcheckbox constructor Function
Jcheckbox (): Establish a new jchcekbox.
Jcheckbox (icon icon): Create a jcheckbox with images but no text.
Jcheckbox (Icon Icon,boolean selected): Creates a jcheckbox with an image but no text, and sets its initial state (with or without selection).
Jcheckbox (String text): Create a jcheckbox with text.
Jcheckbox (String Text,boolean selected): Creates a jcheckbox with text and sets its initial state (with or without selection).
Jcheckbox (String Text,icon Icon): Creates a jcheckbox with text and images with no initial state selected.

Jcheckbox (String text,icon Icon,boolean selected): Creates a text-based jcheckbox with an image and sets its initial state (with or without selection).

When an option in Jcheckbox is selected or canceled, it triggers the ItemEvent event, ItemEvent This class provides 4 ways to use it, namely GetItem (), getitemselectable (), Getstatechange (), paramstring (). The GetItem () and Paramstring () methods return some status values for this jcheckbox. In general, we seldom use these two methods.

Getitemselectable () is equivalent to the GetSource () method, which is the component that returns the triggering event, which is used to determine if the component generated the event. The GetSource () method is provided by the EventObject class, and all event classes inherit the class, so all events we can use the GetSource () method to determine exactly which component triggered the event.

The last Getstatechange () method returns whether this component is selected at all. This method returns an integer value. We can use the class variables provided by ItemEvent, and if selected, return to selected and return deselected if not selected.

The radio button and check boxes register and unregister the ItemEvent event listener in the following ways:

public void Additemlistener (ItemListener L): registers the specified ItemListener event listener

public void Removeitemlistener (ItemListener L): Unregister the specified ItemListener event listener

Package Ch10;import Java.awt.event.*;import javax.swing.*;p ublic class Vote extends JFrame implements actionlistener{ Private JPanel JP = new JPanel (); Jradiobutton JRB1 = new Jradiobutton ("This site is very good, very novel!") ", true); Jradiobutton JRB2 = new Jradiobutton ("This site is very common, too general"); Jradiobutton jrb3 = new Jradiobutton ("This site is poor, occasionally look"); Jradiobutton jrb4 = new Jradiobutton ("This site is too poor to come"); Private jradiobutton[] jrb = new JRADIOBUTTON[]{JRB1,JRB2,JRB3,JRB4}; Private Buttongroup bg = new Buttongroup (); Jcheckbox JCB1 = new Jcheckbox ("Nice Interface"); Jcheckbox jcb2 = new Jcheckbox ("Richer content"); Jcheckbox jcb3 = new Jcheckbox ("Better value-added service"); Jcheckbox jcb4 = new Jcheckbox ("Better member service"); Private jcheckbox[] JCB =new JCHECKBOX[]{JCB1,JCB2,JCB3,JCB4}; Private JButton [] jb = {New JButton ("I want to vote"), new JButton ("I want to re-elect")}; Private jlabel[] JL = {New JLabel ("This site gives you the impression:"), New JLabel ("Where do you think the site is doing better"), New JLabel ("You Vote for:")}; Private JTextArea JT = new JTextArea (); Private JScrollPane js= New JScrollPane (JT); Public Vote () {Jp.setlayoUT (NULL); for (int i=0;i<4;i++) {jrb[i].setbounds (30+170*i,40,170,30); Jcb[i].setbounds (30+120*i,100,120,30); Jp.add (jrb[i ]); Jp.add (Jcb[i]); Jcb[i].addactionlistener (this); Jrb[i].addactionlistener (this); Bg.add (Jrb[i]); if (i>1) continue; Jl[i].setbounds (20,20+50*i,200,30); Jb[i].setbounds (380+120*i,200,100,20); Jp.add (Jl[i]); Jp.add (Jb[i]); Jb[i].addactionlistener (this); } jl[2].setbounds (20,150,120,30); Jp.add (jl[2]); Js.setbounds (120,150,500,50); Jp.add (JS); Jt.setlinewrap (TRUE); Jt.seteditable (FALSE); This.add (JP); This.settitle ("website satisfaction Questionnaire"); This.setbounds (150,150,750,300); This.setvisible (TRUE); This.setdefaultcloseoperation (Jframe.exit_on_close);     } public void actionperformed (ActionEvent a) {if (A.getsource () ==jb[1]) {bg.clearselection ();     for (int i=0;i<jcb.length;i++) jcb[i].setselected (false);     Jt.settext ("");     } else {StringBuffer Temp1 = new StringBuffer ("You think this site");     StringBuffer temp2 = new StringBuffer (""); Forint i=0;i<4;i++) {if (jrb[i].isselected ()) Temp1.append (Jrb[i].gettext ());     if (jcb[i].isselected ()) Temp2.append (Jcb[i].gettext () + ",");     } if (Temp2.length () ==0) jt.settext ("Please select both surveys");     else {temp1.append ("You think this site");     Temp1.append (Temp2.substring (0,temp2.length ()-1));     Jt.settext (Temp1.tostring ());     }}} public static void Main (String args[]) {new Vote (); }}





radio buttons and check boxes

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.