Java Interface Programming (5)------buttons and button groups

Source: Internet
Author: User

This article is your own study notes, welcome reprint, but please indicate the source: http://blog.csdn.net/jesson20121020

With the layout manager and the Swing event model in view, the rest of Swing's components are now learning the usage and functionality of each of the various components of swing, and this section first looks at the most commonly used buttons and button groups.

1. Button

Swing offers many types of buttons, such as regular buttons, check boxes, radio buttons, and even menu items, all of which are inherited from AbstractButton. such as the inheritance relationship:

As you can see, Jbutton,jmenuitem,jtogglebutton are subclasses of AbstractButton , and we continue to look at their subclasses:

Here is a list of all the buttons, the following shows the use of the various buttons, here no longer implement the listener, you can add as needed.

public class Buttons extends JFrame {private JButton JB = new JButton ("button");p rivate Basicarrowbutton up = new BASICARROWBU Tton (Basicarrowbutton.north), down = new Basicarrowbutton (Basicarrowbutton.south), right = new Basicarrowbutton ( basicarrowbutton.east), center = new Basicarrowbutton (basicarrowbutton.center), left = new Basicarrowbutton ( basicarrowbutton.west);p ublic Buttons () {//TODO auto-generated constructor stubsetvisible (true); SetSize (500,300); SetLayout (New FlowLayout ()), add (JB), Add (New Jtogglebutton ("Jtogglebutton")), Add (New Jcheckbox ("Jcheckbox")), add ( New Jradiobutton ("Jradiobutton")); JPanel JP = new JPanel (), Jp.setborder (New Titledborder ("Basicarrowbutton")), Jp.add (UP), Jp.add (down), Jp.add (left); Jp.add (right); Jp.add (center); Add (JP); JPanel jp1 = new JPanel () Jp1.setborder (New Titledborder ("JCheckBoxMenuItem")); Jp1.add (New JCheckBoxMenuItem (" JCheckBoxMenuItem1 ")); Jp1.add (New JCheckBoxMenuItem (" JCheckBoxMenuItem2 ")); Add (JP1); JPanel JP2 = new JPanel (); Jp2.setborder (New Titledborder ("JRadioButtonMenuItem ") Jp2.add (New JRadioButtonMenuItem (" JRadioButtonMenuItem1 ")); Jp2.add (new JRadioButtonMenuItem ("JRadioButtonMenuItem2")); Add (JP2); JPanel jp3 = new JPanel (), Jp3.setborder (New Titledborder ("JMenu")), Jp3.add (New JMenu ("JMenu1")), Jp3.add (New JMenu (" JMenu2 ")); Add (JP3);} /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubnew Buttons ();}}
To run, such as effect:


It's just that the buttons are displayed on the form, and no event listeners are added to the individual components.

2. Button Group

In order for radio buttons to exhibit an "exclusive" behavior, they must be added to a button group (buttongroup), in fact, any Abstarctbutton object can be added to the group of buttons.

To avoid repeating the code, the following example uses the reflection function to produce several sets of different types of buttons.

public class Buttongroups extends JFrame {private static string[] ids = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; Private JPanel Makepanel (class<? extends abstractbutton> kind,string[] IDs) {Buttongroup bg = new Buttongroup (); JPanel JP = new JPanel (); String title = Kind.getname (); title = Title.substring (Title.lastindexof ('. ') +1); Jp.setborder (title); for (String id:ids) {AbstractButton ab = new JButton ("failed"); try { Constructor ctor = Kind.getconstructor (string.class); ab = (AbstractButton) ctor.newinstance (ID);} catch (Exception e) {System.out.println ("Cat ' t create" +kind); Bg.add (AB); Jp.add (AB);} return JP;} Public buttongroups () {//TODO auto-generated constructor Stubsetlayout (new FlowLayout ()); SetSize (500,300); setvisible (true); Add (Makepanel (Jbutton.class, IDS)); Add (Makepanel (Jtogglebutton.class,ids)); Add (Makepanel (jcheckbox.class , IDs)); Add (Makepanel (Jradiobutton.class,ids));} /** * @param args */public static void main (string[] args) {//TODO auto-generated MeThod stubnew buttongroups ();}} 
where the Makepanel () method is used to create a button group and a JPanel, the second parameter of this method is a string data, for each of these strings, a button instance that is represented by the first argument is created, and then the button is added to the JPanel.

To run the program, the effect is as follows:

The caption of the border is obtained from the name of the class, and the prefix is removed, and the class name is taken out directly. The GetConstructor () method produces an constructor object that accepts an array of the specified type "passed to GetConstructor ()" In the class list as the number of yards, and then only calls Newinstance () and pass the list of actual arguments to it, in this instance, the IDs data string.

To get an "exclusive" behavior from a button, you have to create a group of buttons and then add the button to the button group that you want to have the "exclusive" behavior. Running the program, you can find that other buttons have this "exclusive" behavior in addition to JButton.


Java Interface Programming (5)------buttons and button groups

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.