Jradiobutton (radio button) Add Event Listener

Source: Internet
Author: User


Code:

Import java.io.File; import java.awt.Container; import java.awt.GridLayout; import Java.awt.event.WindowAdapter; Import java.awt.event.ItemListener; import java.awt.event.ItemEvent; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JPanel; import Javax.swing.JRadioButton; import Javax.swing.ButtonGroup; Import javax.swing.BorderFactory; import Javax.swing.ImageIcon; class MyRadio implements itemlistener{private String R    ight = "D:" + File.separator + "right.png";    Private String wrong = "D:" + File.separator + "wrong.gif";    Private JFrame frame = new JFrame ("Test Information");    Private Container cont = Frame.getcontentpane ();    Private Jradiobutton JRB1 = new Jradiobutton ("Male", new ImageIcon (right), true);    Private Jradiobutton JRB2 = new Jradiobutton ("female", New ImageIcon (wrong), false);    Private JPanel pan = new JPanel ();    Public MyRadio () {Pan.setborder (Borderfactory.createtitledborder ("select Gender")); Sets the display bar of a border pan.setlayout (new GrIdlayout (1,3));        Pan.add (THIS.JRB1);               Pan.add (THIS.JRB2);        Buttongroup Group = new Buttongroup ();        Group.add (THIS.JRB1);               Group.add (THIS.JRB2);        Jrb1.additemlistener (this);        Jrb2.additemlistener (this);        Cont.add (PAN);        This.frame.setSize (300,80);        This.frame.setVisible (TRUE); This.frame.addWindowListener (New Windowadapter () {public void windowclosing (WindowEvent obj) {Sy            Stem.exit (1);    }        }) ; } public void itemstatechanged (ItemEvent e) {if (E.getsource () ==jrb1) {Jrb1.seticon (new ImageIcon (righ            t));        Jrb2.seticon (new ImageIcon (wrong));            }else{Jrb1.seticon (New ImageIcon (wrong));               Jrb2.seticon (New ImageIcon (right));    }}};class tester{public static void Main (String args[]) {new MyRadio (); }};


Jradiobutton (radio button) Add Event Listener

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.