Jump to content

First Examples

75% developed
From Wikibooks, open books for an open world


On this page, we'll get started with swing. We'll look at the classes JFrame and JLabel, and build a basic swing "hello world" application.

JFrames

[edit | edit source]

JFrames are swing components. A swing component is a part of a graphic user interface (GUI). Frames, windows, text boxes, buttons, switches and many other parts of a GUI application are all components. The root class of all swing components is the JComponent class, and other swing components, including JFrame, are subclasses of JComponent. JComponent is an abstract class, so it cannot be instantiated directly, but can be subclassed, which is useful if you want to write your own custom GUI component.

A JFrame is a top-level component, meaning that it contains other components, but is not contained itself. It's onscreen appearance is dictated by the platform, but generally it is a window that the user can resize, move, maximize, minimize, and has its own title bar.

Probably the best way to get familiar with a JFrame, as well as components in general, is to run a short example program that creates one. The following program will do the trick.

Computer code A simple Swing application
import javax.swing.JFrame;  // All swing components live
                    // in the javax.swing package


// A simple class to test a JFrame
public class JFrameTest {
    
    public static void main(String[] args) {
        
        // create the frame.  The constructor optionally takes
                // a string as an argument that's used as the title.
        JFrame frame = new JFrame("This is a test!");
        
        // set the size -- 300 by 300 is good
        frame.setSize(300, 300);
        
        // this next line is currently commented.  If you
        // uncomment it, the user will not be able to resize
        // the JFrame

        // frame.setResizable(false);
        
        // has the application exit when the frame is closed
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // makes the frame visible.  The frame is invisible
        // until this method is called.
        frame.setVisible(true);
        
        // these lines are also commented, but you can uncomment
        // them to see their effects.  The first will center the
        // JFrame in the center of the screen.  The second will
        // make it the front, focused window.

        // frame.setLocationRelativeTo(null);
        // frame.toFront();
    }
}

The code creates an empty JFrame. When you run it, you should see a window appear on your screen. The code also illustrates some methods that can be used with JFrames, some of which are commented out. I would encourage you to experiment a bit with commenting and uncommenting these, and seeing the effect.

JLabels

[edit | edit source]

An empty frame is only so exciting, so for this section, we're going to look at JLabels, and put one in a JFrame. A JLabel is a GUI component that can hold an image, some text, or both.

The following code creates a GUI version of the "Hello World!" application.

Computer code Hello world in Swing
import javax.swing.*;  // All swing components live
               // in the javax.swing package

// A GUI hello world application
public class Hello {

    public static void main(String[] args) {
        
        // creates the label.  The JLabel constructor
        // takes an optional argument which sets the text
        // of the label.
        JLabel label = new JLabel("Hello, World!");
        
        // The next line is commented out.  If it is
        // uncommented, the text will be aligned with
        // the center of the frame, otherwise it will
        // align on the left.
        
        // label.setHorizontalAlignment(SwingConstants.CENTER);
        
        JFrame frame = new JFrame("Hello");
        frame.add(label);
        
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.toFront();
    }
}

This program is pretty much like the previous one, except that it creates a JLabel with the text "Hello, World!" in addition to the JFrame, and uses the JFrame add() method to add it to the frame before making it visible. If you compile and run the program as-is, the "Hello, world" text will be aligned on the left side of the window.

By uncommenting the line label.setHorizontalAlignment(SwingConstants.CENTER); you can move the text to the center.

Summary

[edit | edit source]

We've seen how to create a JFrame, a top level container for a swing GUI application, as well as a few JFrame methods that can be used to change its settings. We also looked at the JLabel class, and used the JFrame's add() method to add a JLabel to the frame in order to create a simple application.

Looking at these examples, you might be wondering what other methods exist for JFrames, JLabels, and other components. Although we'll continue to look at more components and more methods. Fortunately, Oracle provides an invaluable online reference to all of the core Java classes, including swing, which can be viewed at the following link:

http://docs.oracle.com/javase/7/docs/api/index.html


Clipboard

To do:
Add screenshots.