Java Swing – JLabel(Part 2)

Today we will continue to learn a little bit more about the magnificent JLabel. Like I had said before not only can we use it to display text, but we can also add some images to it. Isn’t that exciting! Ok, well I’m not sure if you are as excited as I am about this, but hopefully you are. Think of all of the possibilities. Imagine all of the pictures you have wanted to add to your applications, but weren’t able to because you weren’t sure how to do that, or if you already knew how to add images to your applications using some other way now you will also know how to do it using the almighty JLabel class. Lets get started by looking at some code.

JLabel Image Example:

 

 

* When I created the JLabel object here, I was able to create it with an ImageIcon object as the parameter of the JLabel object. By doing that it creates a JLabel with the image inside of it.

There you have the JLabel with an image. Isn’t it great! I’m not the only one who thinks this is cool by the way. We have the monkey looking and admiring your coding skills.

The fun doesn’t stop there because the JLabel can actually have some more spice added to it by having both text and an image.

JLabel Image and Text Example:

There we have an example showing both text and a picture on the JLabel, and look even the monkey thinks this is “awesome!”

This is a basic example using both text and a picture because guess what! You can actually use the text customization discussed in JLabel(Part 1). If you missed that post and want to learn about some text customizations I talked about, just click on this link. That being said I think I have rambled enough about the JLabel for now, so don’t forget to mess around with the code a bit and see what great things you can create. Have fun!

Leave a comment