Swing Learning Note 1-----The hierarchy of Swing component classes

Source: Internet
Author: User

1. Structural Partitioning

There are two types of Swing components, one is the JComponent class and one is the Windows class. Where the Windows class contains components that can be displayed independently, and the JComponent class contains components that cannot be displayed independently .

What are components that can be displayed independently, and components that cannot be displayed independently?

components that can be displayed independently : When you run a program, components that can be displayed independently do not have to be displayed on other components, that is, it can be displayed directly, such as the JFrame class.

components that cannot be displayed independently : At run time, you must rely on components that can be displayed independently to display them, such as the JLabel class, the JButton class, which you have to entrust to a class similar to JFrame to display.

2. From the functional division

Functionally divided into: top-level components, intermediate components, and basic components.

top-level container : Jframe,jdialog,japplet,jwindow. The so-called top-level container, which is the window component that was previously spoken, can be displayed independently.

Intermediate container : Jpanel,jscrollpane,jsplitpane,jtoolbar. The so-called intermediate container means those components that can act as vectors but cannot be displayed independently, that is, some basic components can be relied upon, but they cannot be displayed independently. Must be based on the top-level container.

Special Container : The middle layer of its special function on the GUI, such as Jinternalframe,jlayeredpane,jrootpane. Here the special container is actually a kind of intermediate container class, but in the graphics more can play a professional and beautification role.

Basic Components : components that can play human-computer interaction, such as Jbutton,jlabel,jcombobox,jlist,jmenu,jslider,jtextfield.

Note: To add a basic component, be sure to add an intermediate container to host it.

The following example can describe the problem well:

 Public class Test {    publicstaticvoid  main (string[] args) {        new JButton ("test");}    }

This code runs without any display.

Look at this code again.

 Public class Test {    publicstaticvoid  main (string[] args) {        new JFrame ("test");         New JButton ("test");        Jbutton.setsize (10,20);        Jframe.add (JButton);        Jframe.setsize (400,300);        Jframe.setvisible (true);        Jframe.setdefaultcloseoperation (Windowconstants.exit_on_close);    }}
Operation Result:
Although the size of the JButton is set to 10,20, it is still filled with the whole frame.
In order to achieve the JButton size of 10, 20, an intermediate container must be added to carry the line.
Then look at the following code:
 Public classTest { Public Static voidMain (string[] args) {JFrame JFrame=NewJFrame ("Test"); JButton JButton=NewJButton ("Test"); JPanel pane=NewJPanel ();        Jframe.setcontentpane (pane); Jbutton.setsize (10,20); Jframe.setsize (400, 300);        Pane.add (JButton); Jframe.setvisible (true);    Jframe.setdefaultcloseoperation (Windowconstants.exit_on_close); }}

Operation Result:

This is what we are designed to achieve.

Swing Learning Note 1-----The hierarchy of Swing component classes

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.