GUI 4 - Tiny Window with a JLabel

Purpose of this lesson: New Java classes, constructors, and methods

The main program is like the previous example, after changing the GUI class name, so it isn't repeated here.

JFrame constructor and methods

JFrame
JFrame()
setDefaultCloseOperation(...)
setTitle(title)
setVisible(true)
setContentPane(layout)
pack()
setLocationRelativeTo(null)
The previous example called the JFrame constructor, activated the close box, set the title bar, and made the window visible.

This example shows how to center the window on the screen, and work with the content pane. We'll create a JPanel with the components and then use setContentPane to make it the window's content pane. After everything has been added to the window, it's necessary to call pack() to perform the layout process. Then we can call setLocationRelativeTo(null) to center the window.

JPanel constructor and methods

JPanel
JPanel()
setLayout(layout)
add(component)
JPanel is used primarily for holding components, so there are few operations that it performs. You need to call the constructor, tell what layout to use, and add the components.

JLabel constructor and methods

JLabel
JLabel(text)
JLabel(image)
JLabel(textimage)
setFont(font)
setText(text)

Usage: Because there is usually no need to refer to a label again, it is common to pass it directly to the add() method without saving it in a variable. The constructor is the only thing you really have to know about JLabel.

content.add(new JLabel("Name"));

If you want change the font (or text), save the JLabel in a variable.

FlowLayout - Left to right, top to bottom

Making a good component layout can be a lot of work. Java uses layout managers to implement various layout strategies. The simplest is FlowLayout, which simply centers the components adding them from left to right, and if necessary flowing them to another line. This example only shows one component, but you'll see more in the next example. The only thing you do with a layout object is create it with new and use it to set the layout of a JPanel.

Source code - a subclass of JFrame

Here's the source code, which goes through all the normal steps in building a GUI, even though we're adding only one component, a JLabel, to it.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
// File   : gui-tutorial/tw4/TinyWindow4.java
// Purpose: Create a window with a JLabel.
// Author : Fred Swartz - 2006-11-09

import java.awt.*;           // Needed for FlowLayout.           //Note 1
import javax.swing.*;

////////////////////////////////////////////////////// class TinyWindow4
class TinyWindow4 extends JFrame {

    //============================================================= main
    public static void main(String[] args) {
        TinyWindow4 window = new TinyWindow4();
        window.setVisible(true);
    }

    //======================================================= constructor
    public TinyWindow4() {
        //... Create content panel, set layout
        JPanel content = new JPanel();                           //Note 2
        content.setLayout(new FlowLayout());   // Use FlowLayout //Note 3

        //... Add one label to the content pane.
        JLabel greeting = new JLabel("We come in peace.");       //Note 4
        content.add(greeting);                 // Add label      //Note 5

        //... Set window (JFrame) characteristics
        setContentPane(content);                                 //Note 6
        pack();                                // Do layout.     //Note 7
        setTitle("Tiny Window using JFrame Subclass");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);           // Center window.
    }
}

Notes

  1. Some elements of the GUI interface are defined in the older AWT package, although the components and containers (starting with "J") are all part of Swing.
  2. Create a new JPanel to hold the components for the window's content pane.
  3. A layout manager should be explicitly set for the content pane.
  4. Create a JLabel.
  5. Add the label to the content pane. There really was no reason to assign the JLabel to a variable - the new JLabel(..) could have been an argument to add(...).
  6. The content pane of this JFrame is now set to the JPanel that we created earlier, named "content".
  7. After the content pane of the JFrame has been set and populated with components, it's necessary to call pack() to do the layout -- it sets the location and size of each component.

Creating a new content pane rather than using the defualt

Every window (JFrame) has a content pane that arranges the components which have been added to it according to the specified layout. The content pane is usually implemented as a JPanel.

Altho every JFrame comes with a predefined content pane, these examples create a new JPanel for the content pane. If you're interested in the issues and options for handling the content pane, look at Content Pane (or Content Pain?), but it isn't necessary.