Java Interface Programming (5), java Interface Programming

Source: Internet
Author: User

Java Interface Programming (5), java Interface Programming

This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020

After learning about the layout manager and the Swing event model, the rest is the various components of Swing. Next we will start to learn the usage and functions of each component of Swing one by one, this section describes the most common buttons and button groups.

1. Button

Swing provides many types of buttons, including common buttons, check boxes, single-choice buttons, and even menu items. All these buttons are inherited from AbstractButton. For example, the inheritance relationship:

We can see that JButton, JMenuItem, and JToggleButton are all subclasses of AbstractButton. Let's continue to look at their subclasses:

All buttons are listed here. The usage of buttons is demonstrated below. Listeners are no longer implemented here. You can add them as needed.

Public class Buttons extends JFrame {private JButton jb = new JButton ("button"); private BasicArrowButton up = new BasicArrowButton (BasicArrowButton. NORTH), down = new BasicArrowButton (BasicArrowButton. SOUTH), right = new BasicArrowButton (BasicArrowButton. EAST), center = new BasicArrowButton (BasicArrowButton. CENTER), left = new BasicArrowButton (BasicArrowButton. WEST); public 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 ("updated"); 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 ();}}
Running, such as effect:


These buttons are only displayed on the form, and no event listener is added to each component.

2. Button Group

To allow a single-choice button to show a certain "exclusive" behavior, you must add them to a "button group". In fact, any AbstarctButton object can be added to the button group.

To avoid repeated code, the following example uses the reflection function to generate several 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(new TitledBorder(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();}}
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 string, create a button instance represented by the first parameter and add the button to the JPanel.

Run the program as follows:

The title of the border is obtained from the class name, And the prefix is removed, and the class name is taken directly. The getConstructor () method generates a Constructor object. The Constructor accepts the array consisting of the specified type "in the Class list" passed to getConstructor () "as the number of codes. Then, you only need to call newInstance () and pass the list of actual parameters to it. In this instance, it is the string of ids Data.

To get the exclusive action through the button, you must first create a button group and add the button that you want to have the exclusive action to this button group. Run the program and you can find that all buttons except JButton have this exclusive action.


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.