Java Reference
In-Depth Information
E
XERCISES
Even though you haven't explored in depth how to create a GUI, you are going to
walk through a simple example. To start, copy the file SimpleFrame.java from the
Chapter12 subdirectory in the Exercises directory on the CD-ROM to your
java4cobol directory.
ON THE DVD
1. Compile the
SimpleFrame
class.
SimpleFrame
takes care of the work of cre-
ating a frame window and various listeners so that the window will re-
spond properly to messages. It also provides some utility methods for
creating buttons and menu items. You won't be using a menu in this exer-
cise, but you will be creating a button.
2. Create a new Java file called MyFrame.java. Add the following import state-
ments to the file.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
3. Write the code to create the
MyFrame
class. Notice that
MyFrame
must extend
SimpleFrame
for this exercise to compile correctly.
public class MyFrame extends SimpleFrame
{
}
4. Add a constructor. This constructor invokes the constructor in Simple-
Frame. The text “My Frame” will become the title on the window.
public MyFrame()
{
super("My Frame");
}
5. Override the
buildGUI()
method in
SimpleFrame
. This method is automat-
ically invoked in the
SimpleFrame
constructor.
public void buildGUI()
{
// Call the createButton method in SimpleFrame.
JButton button = createButton("Click me", "Click me");