label_007= new JLabel("My Label");
In lieu of the above Label, My Label
I want to insert an image in the same position.
How can I do that? I am just a novice in Java.
Please help
label_007= new JLabel("My Label");
In lieu of the above Label, My Label
I want to insert an image in the same position.
How can I do that? I am just a novice in Java.
Please help
ImageIcon icon = new ImageIcon('image.png');
JLabel image = new JLabel();
image.setIcon(icon);
The above code should work.
You can use ImageIcon
for images:
ImageIcon ICON = new ImageIcon(URLClassLoader.class.getResource(IMAGE_PATH));
JLabel imageLabel = new JLabel();
imageLabel.setIcon(ICON);
URLClassLoader.class.getResource is using for packaging the application in runnable-jar file. The IMAGE_PATH is relative to your project folders/packages. for example if the image is in com.app.images package, the path will be /com/app/images/image.png.
If you're novice in Java and particularly in Swing, I encourage you to check my swing projects- Minesweeper and Pacman. They are well-documented, I built them when I learned Swing.
Good luck :)