Home > Java > JavaBase > How to add pictures in java form program

How to add pictures in java form program

王林
Release: 2019-12-18 14:34:37
Original
16550 people have browsed it

How to add pictures in java form program

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:

How to add pictures in java form program

Write the content as shown in Window.java and set the form Some basic information

How to add pictures in java form program

# 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.

How to add pictures in java form program

After adding the main function, the file content of Window.java is as shown in the figure:

How to add pictures in java form program

How to add pictures in java form program

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.

How to add pictures in java form program

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.

How to add pictures in java form program

After modification, run the program again, the result is as shown:

How to add pictures in java form program

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template