Open eclipse and create a java project. After creation, create a new class Window under src. Since pictures need to be inserted, a folder imgs is also created in the project directory. Place a 60*60 picture. The created project directory and picture are as shown in the picture:
Write the content as shown in Window.java and set the form Some basic information
# Let’s first talk about the method of inserting pictures. Here, the picture is displayed on the label control, and then the label control is placed on the form.
Free video tutorial learning: java learning
Add the code as shown in the constructor of the Window class.
Explain this code: First, read the block.png in the imgs folder through ImageIO.read(), and then getContentPane().setLayout(null); sets the layout of the panel to null, that is No layout manager is used.
Then create a label object and place the image on the label. After creating the label object, first place the label on the panel, and then set the label's position and size through setBounds().
Note that you must first place the label on the panel and then set the label's position.
After adding the main function, the file content of Window.java is as shown in the figure:
Run Window.java, the result is as shown in the figure. You can see that the inserted picture is displayed in the upper left corner of the form.
There are four parameters in setBounds(), which are the x value, y value of the object placement, and the width and height of the object.
If you want to put the picture in the middle of the form, you can modify the code as shown.
After modification, run the program again, the result is as shown:
Recommended related articles and tutorials: javazero Basic introduction
The above is the detailed content of How to add pictures in java form program. For more information, please follow other related articles on the PHP Chinese website!