Swing Common components radio buttons and check boxes _java

Source: Internet
Author: User
Tags event listener gettext stringbuffer

This article for everyone to share the Swing radio button and check box to use the method for your reference, the specific content as follows

Jradiobutton constructor:

Jradiobutton (): Create a new Jradiobutton.
Jradiobutton (icon icon): Create a Jradiobutton with images but no text.
Jradiobutton (Icon icon,boolean selected): Create a Jradiobutton with an image but no text, and set its initial state (with or without it being selected).
Jradiobutton (String text): Creates a Jradiobutton with text.
Jradiobutton (String Text,boolean selected): Creates a text-Jradiobutton and sets its initial state (with or without it being selected).
Jradiobutton (String Text,icon Icon): Creates a text-and-image Jradiobutton with an initial state of no selection.
Jradiobutton (String text,icon Icon,boolean selected): Creates a text-and-image Jradiobutton and sets its initial state (with or without selection)
To change the RadioButton to a single election, we must use the Buttongroup class. This class is located under the package of javax.swing, and the main function of the Buttongroup class is that only one component at a time will have a status of "on" and the other is "off", i.e. only one component will be selected at the same time. The Buttongroup class can be used by subclasses below AbstractButton, most commonly used Jradiobutton, Jradiobuttonmenu, item, and Jtogglebutton components

The Buttongroup class is constructed in the following ways:

Buttongroup () Creates a new Buttongroup ()

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

Jcheckbox Constructors

Jcheckbox (): Create a new jchcekbox.
Jcheckbox (icon icon): Create a jcheckbox with images but no text.
Jcheckbox (Icon icon,boolean selected): Create a jcheckbox with an image but no text, and set its initial state (with or without it being selected).
Jcheckbox (String text): Creates a jcheckbox with text.
Jcheckbox (String Text,boolean selected): Creates a text-Jcheckbox and sets its initial state (with or without it being selected).
Jcheckbox (String Text,icon Icon): Creates a text-and-image jcheckbox with an initial state of no selection.
Jcheckbox (String text,icon Icon,boolean selected): Creates a text-and-image Jcheckbox and sets its initial state (with or without it).
When an option in Jcheckbox is selected or canceled, it triggers itemevent events, itemevent This class altogether provides 4 methods that can be used, namely GetItem (), getitemselectable (), Getstatechange (), paramstring (). The GetItem () and Paramstring () methods return some of the Jcheckbox's state values. Generally we use less of these two methods.

Getitemselectable () is the equivalent of the GetSource () method, which returns the component that triggers the event, which is used to determine which 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 which component is triggering the event.

The

Last Getstatechange () method returns whether this component has been selected in the end. This method returns an integer value. And we can use the class variables provided by ItemEvent, if selected, return selected, and return deselected if not selected. The
radio buttons and checkboxes register and unregister ItemEvent event listeners as follows:
public void Additemlistener (ItemListener L): Registering 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.*; 
   public 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 very bad, occasionally look at"); 
   Jradiobutton jrb4 = new Jradiobutton ("This site is too bad, not Come"); 
   Private jradiobutton[] jrb = new JRADIOBUTTON[]{JRB1,JRB2,JRB3,JRB4}; 
   Private Buttongroup bg = new Buttongroup (); 
   Jcheckbox JCB1 = new Jcheckbox ("More Beautiful Interface"); 
   Jcheckbox jcb2 = new Jcheckbox ("relatively rich 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 be re-elected")}; 
   Private jlabel[] JL = {New JLabel ("This site gives you the impression is:"), New JLabel ("Where do you think the site is doing better"), New JLabel ("Your vote is:")}; 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 Survey form"); 
     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 (""); 
         for (int 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 ();
 } 
}

Effect Chart:


The above is the entire content of this article, I hope to help you learn.

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.