When you beautify a program, you often need to add a background picture on the form. By searching and testing, 2 effective ways were found. The following are described separately.
1. Loading images with JLabel
Load the icon with JLabel's SetIcon (icon icon) and set the position and size of the JLabel object to completely overwrite the form. This is a very trickery approach, the code is very simple, as shown below.
New JLabel (ImageIcon); Lbbg.setbounds (0, 0, Framesize.width, framesize.height); this. Getcontentpane (). Add (LBBG);
However, there are several points to note about this approach:
(1) Cannot use Layout Manager
At this point you need to set the layout manager to null, and then precisely control the size and position of all controls. Otherwise, JLabel cannot completely overwrite the form.
(2) You should add the background JLabel and add the other controls first. Otherwise, the other controls will be obscured by jlabel (why not add masks first?). )。
(3) The background image cannot be scaled because controls and form dimensions need to be manually controlled.
2. Paintcomponent (Graphics g) method for overloading JPanel
By overloading the method, the specified picture is drawn up at the drawing stage of the JPanel. Because the background is drawn, it does not have any effect on the layout.
The sample code is as follows:
@Override Public void paintcomponent (Graphics g) {superthis); Mainframe.instance (). repaint ();}
The following is a complete demo.
Packageframe;Importjava.awt.Dimension;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJavax.swing.ImageIcon;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JTextField; Public classImageFrameextendsJFrame {classImagepanelextendsJPanel {Dimension D; Image Image; PublicImagepanel (Dimension D, image image) {Super(); This. D =D; This. Image =image; } @Override Public voidpaintcomponent (Graphics g) {Super. paintcomponent (g); G.drawimage (Image,0, 0, d.width, D.height, This); Mainframe.instance (). repaint (); }} Dimension framesize=NewDimension (500, 300); ImageIcon ImageIcon=NewImageIcon ( This. GetClass (). GetResource ("/images/bg.jpg")); PublicImageFrame () {//Set Form PropertiessetSize (framesize); Setlocationrelativeto (NULL); Setdefaultcloseoperation (Jframe.exit_on_close); Seticonimage (Imageicon.getimage ()); Setundecorated (true); } Public voidaddimagebyjlable () {setlayout (NULL); //Set BackgroundJLabel LBBG =NewJLabel (ImageIcon); Lbbg.setbounds (0, 0, Framesize.width, framesize.height); This. Getcontentpane (). Add (LBBG); Addcomponents (); SetVisible (true); } Public voidAddimagebyrepaint () {Imagepanel Imagepanel=NewImagepanel (Framesize, Imageicon.getimage ()); Setcontentpane (Imagepanel); Addcomponents (); SetVisible (true); } Private voidaddcomponents () {JButton btn1=NewJButton ("haha"); Btn1.setbounds (10, 20, 60, 30); This. Getcontentpane (). Add (BTN1); JTextField JTF=NewJTextField ("22222222222"); Jtf.setbounds (200, 100, 80, 30); This. Getcontentpane (). Add (JTF); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubImageFrame ImageFrame =NewImageFrame (); //imageframe.addimagebyjlable ();Imageframe.addimagebyrepaint (); }}
The results are as follows:
Figure 1 Loading picture effects with JLabel
Figure 1 shows that when using JLabel, the image is only part of the picture because no picture size is inconsistent with the form size, and one of the controls is obscured. Note: You can achieve a more satisfying effect by fine-setting the size and adding the control order.
Figure 2 Loading a picture using redraw mode
As shown in Figure 2, it is not necessary to set the matching dimensions and the order in which the controls are added to achieve a satisfactory result.
2 ways to add a background picture to a swing-form