Java Swing – JLabel

I am very excited to tell you that today we will final get to make some magic happen. You might be thinking something like “ how will I (insert your name here) make magic happen; ” don’t worry we won’t be pulling bunnies out off hats, nor will we being doing an strange illusion like the one where someone gets in a box and they get sawed in half, but then it turns out they were never actually sawed in half. That is such a cool illusion, but it is also a little creepy; however, enough of that because today we will start doing our own magic, and yes it involves our GUI’s. We will officially start displaying things on our JPanels. We will start be displaying some text. Yes, this is so exciting!

In order to display text on our GUI’s we will use an object named JLabel. JLabel is on of the many ways we can display things on our screen. In this case we are displaying some text, but JLabel can also be used to display images, but I’ll get into images in a little more detail later on. Lets focus on the awesome text.

JLabel Text Example:

* We instantiate a JLabel object without any parameters and name it text.

** The setText() method takes a string parameter which will be displayed on the JLabel. You can insert any string you would like; however, I used the string “Isn’t this awesome! :)”.

*** Now that the JLabel is ready to go we must add it to the JPanel.


It’s alive! Think of all of the things you can now display on your computer. Okay displaying black text on the computer screen is pretty cool, but it doesn’t stop their. We can customize the text a bit.

JLabel Custom Color:

 

* SetForeground() method is going to allow you to change the color of the text to whatever color you want; it takes a Color object as a parameter. However, in order to use the Color class you must import the awt framework onto your program. For this example I used the color red.

Wow, now you can change the color of the text to any color you want.

Jlabel Custom Font and Size:

 

* setFont() takes in a parameter of a font object. I am actually able to create an instance of the font object within the parenthesis of the setFont method. It can be convient beause it will reduce the amount of unnecessary code typed. I know it might not be that big of a deal as we are writing small applications; however, as the appllications get bigger and you add more awesome features there is going to be lots of code, and getting rid of unnecessary code will help keep your application’s bugs to a minimum. Let’s get back to the font object I created within the setFont method, so I created the font object with three parameters. The first parameter is a string which represents the name of a font, the second paramater is an int which represents the style the font will be written in, and the last parameter is the size of the font.

If you run the “custom font and size” code then you should get an application like the one above. It’s pretty cool that you can now display any kind of text you want on the screen. Today I have gone over a lot of information. Take a moment to let it all sink in, and mess around with the code above for a bit. Try displaying different kinds of text on the application. Have fun!

Leave a comment



Java Swing - JTextField - Nerdy Rocks

2 years ago

[…] want to add some text to our applications; I know we have already covered a way to do that with a Jlabel but today I will be showing you a way which is a little different. We will be using an object […]