So I am using Eclipse with Windows builder. I was just wondering if there was anyway I can import an image that'll show up on the JFrame that I can easily move around and re-size instead of setting the location and size and drawing it.
相关问题
- Views base64 encoded blob in HTML with PHP
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- How to get the background from multiple images by
As martijn-courteaux said, create a custom component it's the better option. In C# exists a component called PictureBox and I tried to create this component for Java, here is the code:
If you want to add an image, choose the JPictureBox, after that go to Properties and find "icon" property and select an image. If you want to change the sizeMode property then choose the JPictureBox, after that go to Properties and find "sizeMode" property, you can choose some values:
If you want to learn more about this topic, you can check this video.
Also you can see the code on Gitlab or Github.
If you are using Netbeans to develop, use jLabel and change it's icon property.
There is no specialized image component provided in Swing (which is sad in my opinion). So, there are a few options:
Create in the window builder a JPanel, that will represent the location of the image. Then add your own custom image component to the JPanel using a few lines of code you will never have to change. They should look like this:
where JImageComponent is a self created class that extends
JComponent
that overrides thepaintComponent()
method to draw the image.Here is a simple example of adding an image to a
JFrame
: