Select Page

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:

public static void main(String[] args) {

JFrame frame = new JFrame();

JPanel panel1 = new JPanel();

JLabel text = new JLabel(); // * See Description

text.setText(“Isn't this awesome! :)”); // ** See Description

panel1.add(text); //*** See Description

frame.add(panel1);

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

* 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:

 public static void main(String[] args) {

JFrame frame = new JFrame ();

JPanel panel1 = new JPanel();

JLabel text = new JLabel();

text.setText("Isn't this awesome! :) ");

text.setForeground(Color.red); //* See Description

panel1.add(text);

frame.add(panel1);

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

 

* 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:

public static void main(String[] args) {

JFrame frame = new JFrame ();

JPanel panel1 = new JPanel();

JLabel text = new JLabel();

text.setText("Isn't this awesome! :) ");

text.setForeground(Color.red);

text.setFont(new Font("Serif", Font.ITALIC, 30)); // * See Description

panel1.add(text);

frame.add(panel1);

frame.setSize(400, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

 

* 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!