GUI02

Directions

When creating a GUI program you must first create the frame window. The next step is to add graphical components to the frame. Graphical components consist of things like labels, buttons, text fields, text areas, combo boxes, check boxes, radio buttons, sliders, and panels. In this lesson we learn how to add labels to our frame.

A JLabel is a swing component that is a display area for short text or an image. Adding a JLabel to a frame is a four step processes. This same four step processes will be used later to add other graphical components to the frame.

  1. Create a JLabel variable
  2. Create a JLabel object and store its reference in the JLabel variable
  3. Set the attributes for the label (location, size, color, etc.)
  4. Add the JLabel to the content pane of the frame
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class GUI02 extends JFrame
{
    // Step 1 : create JLabel variable
    private JLabel label;
    
    // constructor
    public GUI02()
    {   
        // Step 2 : create JLabel object and store its reference in label
        label = new JLabel("This is a test");
        
        // Step 3 : set label attributes
        label.setLocation(50, 200);
        label.setSize(600, 50);   // label's footprint - the size of the
                                  // rectangle the label is displayed in
        label.setForeground(Color.BLUE);
        label.setFont(new Font("Arial", Font.PLAIN, 24));
        
        // Step 4: add label to content pane of frame
        getContentPane().add(label);
        
        // set frame attributes
        setLayout(null);                                 
        setSize(600, 480);                              
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        getContentPane().setBackground(Color.white);     
        setVisible(true);                            
    }
    
    // main method
    public static void main(String[] args)
    {
        GUI02 app = new GUI02();   // run program
    }
}   
   

Before we examine the JLabel code it must be pointed out that frame attributes have not changed. You will notice, however, that the code is located at the bottom of the constructor. In our GUI program ordering you will always put the code for the graphical components above the code that sets the attributes for the frame.

Step 1 : create a JLabel variable

private JLabel label;

The JLabel variable label is used to store a reference to the JLabel object so that the object can be accessed in our GUI program. All graphical component variables should be declared as instance variables.

Step 2 : create a JLabel object and store its reference in the JLabel variable

label = new JLabel("This is a test");

To create a JLabel object you use the keyword new followed by JLabel. The parentheses contain the text that will be displayed on the screen.

Step 3 : set the attributes for the label (location, size, color, etc.)

label.setLocation(10, 10);
label.setSize(600, 50);
label.setForeground(Color.BLUE);
label.setFont(new Font("Arial", Font.PLAIN, 24));

setLocation - determines the location with the frame where the label will be displayed.
setSize - sets the size of the label. The size is dependent upon the size of the text in the label and the font size being used.
setForeground - sets the color of the label's text.
setFont - sets the font that will be used to display the text. When setting the font you must create a new font object and provide the font name, font style, and font size.

There are three font style attributes.

Font Style Description
PLAIN
The plain style constant (Font.PLAIN)
BOLD
The bold style constant. (Font.BOLD)
ITALIC
The italicized style constant (Font.ITALIC)

Here are the established baseline fonts: SansSerif, Serif, Dialog, DialogInput, Monospaced. These are not system dependent.

Example

Font myFont = new Font("Serif", Font.PLAIN, 18);

You can also use the system specific font names like Times New Roman and Arial which are defined in the Windows operating system.

Example

Font myFont = new Font("Times New Roman", Font.BOLD, 24);

Step 4 : add the JLabel to the content pane of the frame

getContentPane().add(label);

To add a graphical component to the content pane of the frame you use the add method.

Exercise

There are two sets of instructions depending upon whether you are using Replit or Netbeans as your IDE (editor).

Replit

  1. Logon to replit.

  2. Create a new Java repl named "GUI02".

  3. In the File section of the replit window, click the Add File icon.

  4. Name the newly created file: GUI02.java. You should now have two java files: Main and GUI02.

  5. Copy the code located at the top of the GUI02 assignment page on my website and paste it in the file GUI02.java.

  6. Select the Main.java file. Delete all of the code currently in this file.

  7. Copy the following code and paste it in Main.java file:

    class Main
    {
        public static void main(String[] args)
        {
            GUI02 app = new GUI02();    // run program
        }
    }
    
  8. Run the program.

NetBeans

  1. Open your NetBeans workspace.

  2. Add a new empty Java file named GUI02 to your project.

  3. Copy the code located at the top of the GUI02 assignment page into this new file.

  4. Run the program.

Modifications

  1. Change the text of the label to "Have a nice day!".

  2. Center the label in the middle of the frame window.

  3. Change the font attributes for the label. Adjust the size of the label as needed.

    Replit Users: You may have to adjust the window size by pulling the horizontal and vertical bars to make the GUI window match the size and location numbers you used.

  4. Change the color of the label.

Here is the list of the preset colors.

Color Command
black Color.black
blue Color.blue
cyan Color.cyan
gray Color.gray
green Color.green
magenta Color.magenta
orange Color.orange
white Color.white
yellow Color.yellow

Source File

GUI02.java

Sample Run