Java Swing Interface Programming (29) --- JCheckBox event processing

Source: Internet
Author: User

The event processing listening interfaces of JCheckBox and JRadioButton are the same. They all use the ItemListener interface.

Package com. beyole. util; import java. awt. container; import java. awt. gridLayout; import java. awt. event. itemEvent; import java. awt. event. itemListener; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import java. io. file; import javax. swing. borderFactory; import javax. swing. imageIcon; import javax. swing. JCheckBox; import javax. swing. JFrame; import javax. swing. JPanel; class MyItemListener implements ItemListener {private String right = "f:" + File. separator + "2.png"; private String wrong =" f: "+ File. separator + "3.png"; public void itemStateChanged (ItemEvent e) {JCheckBox jcb = (JCheckBox) e. getItem (); // forcibly convert the event to the JCheckBox class if (jcb. isSelected () {// determines whether jcb is selected. setIcon (new ImageIcon (right); // modify image} else {jcb. setIcon (new ImageIcon (wrong); // modify image }}} class MyCheckBox1 {private String wrong = "f:" + File. separator + "3.png"; // defines the image path private JFrame frame = new JFrame (" Beyole "); // defines the form private Container container = frame. getContentPane (); // get the form container private JCheckBox jcb1 = new JCheckBox ("Main Site", new ImageIcon (wrong); private JCheckBox jcb2 = new JCheckBox ("Forum ", new ImageIcon (wrong); private JCheckBox jcb3 = new JCheckBox ("blog", new ImageIcon (wrong); private JPanel panel = new JPanel (); // define a panel public MyCheckBox1 () {panel. setBorder (BorderFactory. createTitledBorder ("Please select your favorite website"); // defines the display panel of a border. setLayout (new GridLayout (1, 3); // defines the layout of one row and three columns of panel. add (this. jcb1); // Add the component panel. add (this. jcb2); // Add the component panel. add (this. jcb3); // Add component jcb1.addItemListener (new MyItemListener (); // Add listener jcb2.addItemListener (new MyItemListener (); // Add listener jcb3.addItemListener (new MyItemListener ()); // Add the container to the listener. add (panel); // add the panel this. frame. setSize (330,100); // defines the size of the form. this. frame. setVisible (true); // display the form this. frame. addWindowListener (new WindowAdapter () {// Add event listening public void windowClosing (invalid wevent arg0) // Method for closing the rewrite window {System. exit (1); // system exit}) ;}public class JCheckBoxDemo02 {public static void main (String [] args) {new MyCheckBox1 ();}}

The function of this program is very similar to the event listening operation of JRadioButton. After the option is selected, the displayed image is set based on the selected status.

Program:


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.