Javase Swing Program structure, component classification and basic rules

Source: Internet
Author: User

{Related Information}
1. Program Structure
Swing's programming is generally performed in the following process:
(1) Introducing swing package via import.
(2) Set the GUI's "appearance interface Style".
(3) Create a top-level container.
(4) Create components such as buttons and labels.
(5) Add the component to the top-level container.
(6) Add a boundary around the component.
(7) Conduct event processing.

2. Component classification
The JComponent class is derived from the Container class. Not all components of Swing inherit the JComponent class, and any component derived from the Container class can be used as a container. Swing components can be functionally divided into top-level containers, intermediate containers, special containers, basic controls, information display components, and editorial information components.
Top-level containers: Jframe, JApplet, JDialog, JWindow.
Intermediate containers: Jpanel, JScrollPane, JSplitPane, JToolBar.
Special containers: middle layers of special functions in the GUI, such as JInternalFrame, JLayeredPane, JRootPane.
Basic controls: Basic components of human-computer interaction, such as Jbutton, JcomboBox, Jlist, JMenu, JSlider, and JTextField.
Information Display components: Components are displayed only for informational, but not editable, such as Jlabel, JProgressBar, and ToolTip.
Edit information component: Displays to the user components that can be edited, such as JColorChooser, Jfilechoose, JFileChooser, Jtable, JTextArea.

In addition, some special features of the JComponent class include border settings, double buffers, hint information, keyboard navigation, and support layouts.
Border settings: Use the SetBorder () method to set the perimeter border of the component and leave blank for the perimeter of the component if you do not set a border.
Dual buffers: To improve the display of the components, the use of double buffering technology. The JComponent component is double-buffered by default, and you do not have to write your own code, and you can turn off double buffers by setdoublebuffered (false).
Tip: The Settooltiptext () method sets the prompt information for the component and provides help for the user.
Keyboard navigation: Registerkeyboardaction () method can implement keyboard instead of mouse operation.
Support Layouts: Users can set the maximum, minimum, and set alignment parameter values for the component, specifying constraints for the layout manager.

3) Basic rules for using swing
Unlike AWT components, Swing cannot add components directly to the top-level container. Swing components must be added to the content panel associated with the top-level container, which is a common lightweight component and avoids the use of non-Swing lightweight components. There are two ways to add components to the top-level container JFrame object:
(1) using the Getcontentpane () method to obtain the contents of the container panel, directly add components, the format is as follows:

// Get window Contents Panel New JPanel ();  // Create Panel c.add (pane);  // Add a panel to a container


(2) Create an intermediate container object (Jpanel or JDesktopPane), add the component to the intermediate container object, and then set the container as the content panel of the top-level container frame through the Setcontentpane () method.

New JPanel ();p ane.add(new JButton ("OK")); Frame.setcontentpane (pane);


{Related Code}

Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;/*** Created by Re-x on 11/4/14.*/ Public classswingapplication { Public Static voidMain (string[] args) {//2) Set GUI looking style        Try {            //Set window styleUimanager.setlookandfeel (Uimanager.getcrossplatformlookandfeelclassname ()); }        Catch(Exception e) {e.printstacktrace (); }        //3) Create top container        FinalJFrame frame =NewJFrame ("Swing app"); Container C=Frame.getcontentpane (); JPanel Panel=NewJPanel ();        C.add (panel); Panel.setlayout (NewFlowLayout ()); //4) Create component        FinalJLabel label =NewJLabel (); Button Button=NewButton ("Click Me");        Panel.add (label);        Panel.add (button); //5) Action ProcessButton.addactionlistener (NewActionListener () {@Override Public voidactionperformed (ActionEvent e) {Label.settext ("Welcome");        }        }); Frame.addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {Super. windowclosing (e);                Frame.dispose (); System.exit (0);        }        }); Frame.setsize (300, 250); Frame.setvisible (true); }}

Javase Swing Program structure, component classification and basic rules

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.