Java Experience--swing

Source: Internet
Author: User

1. Swing is an "enhanced" GUI component library based on AWT, which replaces most of the weight components of AWT with lightweight components and provides a few additional components and perception control components that AWT lacks to better reflect platform independence.

2. The weight component completes the work by entrusting the operating system counterpart components (i.e., user interface parts of the Windows operating system), including component drawing and incident response. AWT components are all weight components, or AWT components are simply encapsulation of local components. The defects of the weight components are expensive and inefficient, each component will invoke the underlying platform function to draw separately, and because of the constraints on the components of the underlying platform, it has serious platform dependencies, its display effect on different platforms is difficult. ,

3. The lightweight component does not have a local counterpart component that is drawn by drawing in its container window through Java drawing techniques because it is painted. Therefore, it can realize the "transparent" effect of the component, and it can achieve the performance on different platforms, and the cost of component drawing and event processing is much smaller, which improves the efficiency of program running.

4. Because lightweight components must be drawn in their own windows and eventually included in a weight container, several top-level containers in swing components, such as JFrame, JDialog, and japplet, use the weight component and the rest are lightweight. The difference between the weight component and the lightweight component is generally not seen from the use effect. But the actual development of the top container can only be weight components, do not recommend light, weight components mixed use, or do not advocate awt/swing mixing, which may lead to incompatibilities, and even unexpected consequences.

5. It should be emphasized that swing is based on AWT, and that all swing component classes inherit the container class Java.awt.Container in AWT, so their relationships are leveraged rather than superseded. In contrast, swing components are complex, richer, and more powerful, but are not emphasized in small-scale, rapid application development of graphical user interfaces. It is easier to implement using AWT.

6. The Component class component is the parent class of all swing components, except for a few top-level containers (JFrame, JDialog), where a number of methods are added to achieve enhanced component characteristics.

7.7.JFrame Inheritance expands the Java.awt.Frame class, and its structure is much more complex than the frame. Simply put, JFrame is no longer a single container, but consists of a plurality of different container panels (JRootPane, Grasspane, Layerepane, and ContentPane) that contain relationships between each other. We actually use only the content panel Countentpane, and we're going to add the component to its content panel and don't care about the rest of the underlying structure. ,

8. Another difference with frame is that JFrame implements the Java.swing.WindowConstants interface, which defines an integer constant that controls the operation of the window. Mainly include:

do_nothing_on_alose: The default window Close action is no action, that is, when you click the Close button in the window, nothing is done.

hide_on_alose: The default window close operation is hidden window, which is to hide the current window when you click the Close button on the window.

dispose_on_alose: The default window shutdown action is destroy action, which is to release all native screen resources that are used by this window and its contained subcomponents when the window's Close button is clicked, that is, these component resources will be corrupted. All of the memory they use will return to the operating system and mark them as not visible. You can then call the setvisible (true) method to reconstruct the native resource and display the window and its components again. If necessary, we can also invoke the Dispose () method in the top-level window component Java.awt.Window class in the program to manually release its screen resources.

exit_on_alose: The default window Close action is Exit program, which is to exit the current program when you click the Close button on the window.

9. The label (jlable) component in the Swing class in JFrame is transparent by default, and you can call the Setopaque (True) method to set its opacity.

JButton and button functions are basically the same, but can be achieved with complex display effects, such as: You can use the picture as a button tag, you can easily set the shortcut on the JButton (with the Setmnemonic () method Set) and Add tool tip information. The function of the Settooltiptext () method is to register the text to be displayed in the ToolTip for the component.

11. Timer

The Javax.swing.Tinmer class provides a timer function that triggers the ActionEvent event after a specified time delay to perform the required processing logic. In the Listener processing Method actionperformed () should be given in advance to execute the task code, and then call the timer's start () method to start the timer.

You can specify a delay time when the timer object is created, in milliseconds. By default, the timer repeats its work by looping the event-triggering action at the specified delay time for the interval. If you want the timer to act only once, you can call the Setrepeated (false) method of the Timer object, in addition, you can use the Setinitialdelay () method to set the time for the first delay alone, and then the timer can call the Stop () method to stop running. The event is no longer timed to trigger, and the restart () method can then be invoked to restore its operation.

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.