Java Swing Interface Programming (26) --- single choice button: JRadioButton

Source: Internet
Author: User

The single-choice button is to specify one of Multiple display information. In swing, you can use JRadioButton to complete the operation of a group of single-choice buttons.

 

Package com. beyole. util; import java. awt. container; import java. awt. gridLayout; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import javax. swing. borderFactory; import javax. swing. buttonGroup; import javax. swing. JFrame; import javax. swing. JPanel; import javax. swing. JRadioButton; class MyRadio {private JFrame jFrame = new JFrame ("Beyole"); // defines a form private Container container = jFrame. getContentPane (); // get the form container private JRadioButton jb1 = new JRadioButton (""); // define a single choice button private JRadioButton jb2 = new JRadioButton (" "); // define a single-choice button private JRadioButton jb3 = new JRadioButton ("Forum"); // define a single-choice button private JPanel panel = new JPanel (); /// define a panel public MyRadio () {panel. setBorder (BorderFactory. createTitledBorder ("select your favorite website"); // defines a panel's border display panel. setLayout (new GridLayout (1, 3); // defines the layout, one row and three columns of panel. add (this. jb1); // Add the component panel. add (this. jb2); // Add the component panel. add (this. jb3); // Add the component ButtonGroup group = new ButtonGroup (); group. add (this. jb1); group. add (this. jb2); group. add (this. jb3); container. add (panel); // add the panel this. jFrame. setSize (330, 80); // set the form size this. jFrame. setVisible (true); // display the form this. jFrame. addWindowListener (new WindowAdapter () {// Add event listening public void windowClosing (invalid wevent arg0) {// method of closing the rewrite window System. exit (1); // system exit}) ;}public class JRadioButtonDemo {public static void main (String [] args) {new MyRadio ();}}

One of the code here is worth noting. Without this code, single-choice buttons are not really single-choice buttons. At that time, multiple choice is supported.

 

 

ButtonGroup group = new ButtonGroup();group.add(this.jb1);group.add(this.jb2);group.add(this.jb3);

Here, we create a group for the single-choice button. At this time, there will be no multiple choices.

 

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.