Detailed explanation of two methods of adding background pictures to forms by Java Swing
This article describes two methods of adding background pictures to forms by Java swing. Share with you for your reference, as follows:
When beautifying a program, you often need to add a background picture to the form. Through searching and testing, two effective methods are found. These are described below.
1. Use jlabel to load pictures
Use the seticon (icon icon) provided by jlabel to load the icon, and set the position and size of jlabel object to completely cover the form. This is a very clever way. The code is very simple, as shown below.
However, there are several points to note in this method:
(1) You cannot use the layout manager
At this point, you need to set the layout manager to null, and then accurately control the size and position of all controls. Otherwise, jlabel cannot completely overwrite the form.
(2) You should add the background jlabel first and then add other controls. Otherwise, other controls will be obscured by jlabel (why is it not added later?).
(3) Because the size of the control and form needs to be manually controlled, the background picture cannot be scaled.
2. Overload the paintcomponent (graphics g) method of JPanel
By overloading this method, you can draw the specified picture in the drawing stage of JPanel. Since the background is drawn, it has no effect on the layout.
The example code is as follows:
Here is a complete demo.
The operation effect is as follows:
Figure 1 image loading effect using jlabel
As can be seen in Figure 1, when jlabel is used, only part of the picture is displayed because the size of the picture is inconsistent with that of the window; And one control is blocked. Note: satisfactory results can be achieved by fine setting the size and adding the order of controls.
Figure 2 loading pictures by redrawing
As can be seen from Figure 2, a satisfactory effect can be obtained without setting the matching size and the addition order of controls.
Supplementary note: the difference between swing redraws repaint and updateui
repaint
Public void repaint() redraws this component.
If this component is a lightweight component, this method will call the paint method of this component as soon as possible. Otherwise, this method will call the update method of this component as soon as possible.
Note: more information about the drawing mechanisms used by AWT and swing, including how to write the most efficient drawing code.
updateUI
Public void updateui() notification from uimanager that l & F has changed. Replace the current UI object with the latest version of uimanager. Override: updateui in class JComponent. See also: JComponent updateUI()
Maybe everyone has tried to dynamically add components in swing event listening, but JFrame will not be displayed dynamically. It will only be displayed when it becomes larger and smaller (actually redrawn). Repeat, updateui can, or use validate to find it.
The repaint () method in the API is described in this way. After scheduling all the current unfinished events, the component is redrawn, so the repaint method is not always executed immediately.
There are four key methods for swing redrawing: paint(), repeat(), revalidate(), paintimmediately();
When painting, it will call update(), paint(), paintcomponent(), paintborder(), paintchildren() to paint;
So why does the repaint () method delay?
Calling repaint () causes an area to be added to the redraw list queue and scheduled to be redrawn. Generate a request to prevent it from entering the system event queue. Once the request is processed, the internal tool automatically destroys the paintimmediately () method of the component. The method then immediately performs rendering;
That is to say, generally, repaint () will not be executed immediately. After calling it, there will be a process waiting for processing. But repaint is more efficient, delaying the actual drawing and compressing redundant requests into a single paint call.
So how to solve the problem of its reply () delay?
1. Let it execute immediately: use paintimmediately().
2. Use the invoker (runnable dorun) method in the swingutilities tool class; Write the operation you want to execute after repaint () in the thread to be executed; (it will also add this method to the Java internal event queue, which is behind repaint(), so generally, it will not be executed until repaint() is executed. Although this is effective, it is not ideal.);
More readers interested in Java related content can view the special topics of this site: Java data structure and algorithm tutorial, summary of Java character and string operation skills, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills