8.1 Frames | 381
// Declare a variable of class Container
Container outputPane;
finalString WORDS = "Programming and Problem Solving with Java";
...
// Instantiate a JFrame object
outputFrame = new JFrame();
// Ask the JFrame object to return a content pane Container object
outputPane = outputFrame.getContentPane();
// Specify the action to take when the window is closed
outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Specify the size of the JFrame object
outputFrame.setSize(300, 200);
// Specify a layout manager for the content pane object
outputPane.setLayout(new FlowLayout());
// Add output to the content pane object
outputPane.add(new JLabel("The title of this book is "));
outputPane.add(new JLabel("Introduction to "+ WORDS));
// Make the JFrame object visible on the screen
outputFrame.setVisible(true);
...
The ellipses (... ) in this code segment indicate pieces of the Java application that are yet
to be filled in. Let’s now put all these steps together into a class. We can redo the program
from Chapter 2 that prints a name in two formats, but use a window that the program con-
structs rather than System.out. Thus far, we have not seen how to input values from a win-
dow, so we just supply a name as a series of constants. If you turn back to page 000, you can
compare the differences between this version and our earlier version.
//**
// PrintName application
// This application prints a name in two different formats
//**
importjava.awt.; // Supplies layout manager
importjavax.swing.; // Supplies JFrame class for output display
public classPrintName
{
public static voidmain(String[] args)
{
finalString FIRST = "Herman"; // Person's first name
finalString LAST = "Herrmann"; // Person's last name
final char MIDDLE = 'G'; // Person's middle initial
JFrame outputFrame; // Declare JFrame variable
Container outputPane; // Declare Container variable
String firstLast; // Name in first-last format
String lastFirst; // Name in last-first format