Differences and connections between AWT and swing in Java

Source: Internet
Author: User

Both AWT and swing are packages in Java.

AWT (Abstract window Toolkit): Abstract Windows Toolkit, a package for early authoring of graphical interface applications.

Swing: A newly developed graph-bound bread to solve the problems of AWT. Swing is the improvement and extension of AWT.

AWT and Swing are implemented differently:
       AWT graphical functions have one by one correspondence with the graphics functions provided by the operating system. In other words, when we take advantage of the AWT widget graphical user interface, we are actually using the operating system's graphics library.
       different operating systems The graphics library may not function as well, and the functionality that exists on one platform may not exist on another platform. To implement the concept of "compile, run everywhere" as the Java language claims, AWT has to implement platform independence through sacrificing functionality. Therefore, AWT's graphical function is the "intersection" of the graphical functions of each operating system.
        because AWT relies on local methods to implement functionality, the AWT control is called a "heavyweight control."

       Swing, which not only provides all the features of AWT, but also expands the functionality of AWT with pure Java code.
       For example: not all operating systems provide support for tree controls, and swing uses the basic graphing method provided in AWT to simulate a tree control.
       because swing is implemented with pure Java code, swing controls are common across platforms.
       because swing does not use local methods, swing controls are called lightweight controls.

       The difference between AWT and swing:
       1) AWT is based on the local method of C/s + + programs, which run faster, swing is based on the AWT Java program, it runs relatively slow.
       2) AWT controls may behave differently on different platforms, and swing has a consistent performance across all platforms.

In practice, using AWT or swing depends on the type of platform the application deploys. For example:
1 for an embedded application, the hardware resources of the target platform are often very limited, and the running speed of the application is a crucial factor in the project. In this paradox, the simple and efficient AWT is certainly the first choice for embedded Java.
2 in ordinary pc-based or workstation-standard Java applications, the limitations that hardware resources impose on applications are often not key elements of the project. So in the standard version of Java, Swing is advocated, that is, to implement the application's functionality by sacrificing speed.

In Java, the name of the AWT package is the name of the java.awt,swing package is javax.swing.
The hierarchical relationships between the java.awt and javax.swing two packages are as follows:


The container class is a subclass of the Java.awt.Component class, and the JComponent class inherits from the container class. Therefore, the JComponent class is one of the links between AWT and swing.
All the other swing component classes inherit from the JComponent class, except for the Swing top-level container class (Top level containers), as described earlier, the JComponent class is a subclass of the container class, so All swing components are available as containers.
Swing top-level container classes include JFrame, JDialog, JApplet, and JWindow, which provide a place for other swing components to draw themselves.

  
Swing components can be grouped into the following categories by function:
1, top-level containers: JFrame, JApplet, JDialog and JWindow.
2, intermediate containers: JPanel, JScrollPane, JSplitPane, Jtooibar, etc.
3, Special containers: In the user interface has a special role in the intermediate containers, such as Jlnternalframe, JRootPane, JLayeredPane and Jdestoppane.
4, basic components: The realization of human-computer interaction components, such as button, JComboBox, Just, Menu, Mider and so on.
5, not editable information display components: To display the user can not edit the information components, such as JLabel, JProgressBar and Jtooitip.
6, editable information display components: To show users can be edited the format of information components, such as JTable, JTextArea and JTextField.
7, Special dialog box components: Can directly produce Special dialog box components, such as Jcolorchoosor and JFileChooser.

The 4 top-level container classes on swing inherit the AWT components directly, not from JComponent, respectively: JFrame, JDialog, JApplet, and JWindow.
The top-level container class is not a lightweight component, but a heavyweight component (a peer that needs to be partially delegated to a GUI component on the running platform).


In the top-level container:
1.JApplet can be used as a form for Java applets, but typically use the Java.applet.Applet class to create small applications.
2.JFrame is integrated from the Awtframe class and is typically used as the main form.
3.JDialog the form used to create the dialog box.
4.JWindow is similar to the windows in AWT, but hardly used, as there is not much practical value.

The class name of the swing component and the class name corresponding to the AWT component are basically the same, as long as you add "J" to the original AWT component class name, with the following exceptions:
1, JComboBox: corresponding to the choice components in AWT, but more than the choice component features richer.
2, JFileChooser: For the FileDialog components located in the AWT.
3, Jsrcoiibar: corresponding to the scrollbar in AWT. Note the case differences between the B letters in the two component class names.
4. Jcheckbox: corresponding to the checkbox in AWT. Note the case differences between the B letters in the two component class names.
5. JCheckBoxMenuItem: Corresponds to the Checkboxmenuitem in AWT, note the case difference of B-letter in two component class names.

The upper Jcheckbox and JCheckBoxMenuItem differ from the case of checkbox and Checkboxmenuitem letter B, mainly because of early Java naming is not very canonical.

Related Article

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.