Creating a JFrame

advertisement
Lesson 10: “Hey, where’s my form?”
A.K.A. JFrames for a Little GUI
VB
Java
Form
JFrame
Control
Component
Creating a JFrame
In our study of VB, we used variables to refer to forms and controls. For example, to change the
background color of a form, we wrote: Form1.BackColor = vbRed. When we wrote Form1, we
were using a variable named Form1 to refer to an entire “object” in computer memory that contained
all the properties and subs for that form. In VB, we never had to create the form, or the variable that
named it, with code. The first form was created for us when we started a project, and we simply
referred to it as needed. Kiss your days of ease and luxury goodbye, because…
In Java, you must create all objects from scratch. In other words, before you can access a JFrame’s size,
title or other property, you have to create the JFrame object itself. Furthermore, you will no longer have
the convenient design-time properties box as you did in the VB IDE; instead, you will have to make all
such assignments during run-time with code.
The following code shows you how to create and show a JFrame. Meanwhile, you will begin to learn
how to declare variables that refer to objects (instead of “primitives”: int, double, and boolean).
import javax.swing.*;
public class Gui1 {
public static void main (String [] args) {
JFrame thisIsTheVariableName;
//declaration
thisIsTheVariableName = new JFrame();
//assignment
thisIsTheVariableName.setVisible(true);
}
}
Here are the declaration and assignment statements again:
JFrame thisIsTheVariableName;
Variable Type
//declaration
Variable Name
1|©Joshua Britton
The declaration statement creates a name that will refer to an object. The first word in the statement is
the type of object that the variable will refer to, and the second word is a name that will be used to refer
to the object later in the program. The declaration does NOT create an object. Rather, it creates a
variable name that can refer to a particular type of object.
thisIsTheVariableName = new JFrame();
Variable Name
//assignment
Creates an object of type JFrame to
assign to the variable
The assignment creates an object and connects it to the name created in the declaration.. On the right
side of the equals sign, a new JFrame is created in the computer’s memory. The reference on the left
side of the equals is then assigned to that particular JFrame. From now on, we can manipulate that
JFrame by writing its name, then a dot, then one of its methods.
Often, the declaration and the assignment are combined into one line:
JFrame thisIsTheVariableName = new JFrame();
Now we can finally do something with the JFrame – in this case, make it visible:
thisIsTheVariableName.setVisible(true);
Note that there was one strange, unexplained line of code at the beginning of this example:
import javax.swing.*;
When you want to use some of java’s pre-made objects (such as JFrame) you have to identify the
packages to which the objects belong at the beginning of your class file. JFrame is part of the
javax.swing.* package. One package is always included by default: java.lang.*. That is why we did not
need to import a package to use the Math and String in prior packets: they are both part of java.lang.*.
Using a JFrame’s Methods
JFrames have several “built-in” methods. Examine the three methods demonstrated below.
import javax.swing.*;
public class Gui1 {
public static void main (String [] args) {
JFrame jf = new JFrame();
jf.setSize(300, 100);
2|©Joshua Britton
jf.setTitle("My first frame");
jf.setVisible(true);
//false by default
}
}
To call a JFrame method, write the name of the reference to the JFrame (jf, in the above example) then
a dot, then the method name and any arguments. The three methods shown above should basically
speak for themselves. The setSize dimensions are in pixels.
Constants as Arguments
If we click on the X on the title bar, a JFrame disappears and the program appears to end. However, the
program does NOT end: it merely hids. If we want to change that behavior, we can call JFrame’s
setDefaultCloseOperation method:
import javax.swing.*;
public class Gui1 {
public static void main (String [] args) {
JFrame jf = new JFrame();
jf.setSize(300, 100);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
A JFrame’s setDefaultCloseOperation method requires one parameter, an integer indicating
which operation to execute when the X is clicked. Java provides a set of JFrame constants so that you
do not have to memorize the integers. The default value for the close operation is
JFrame.HIDE_ON_CLOSE. In java, all caps generally indicate a constant. We change the value to
JFrame.EXIT_ON_CLOSE so that the program terminates when the window is closed.
Transition: In VB, you only had to type the name of a constant. In java, you have to type the
name of the class the constant belongs to, then a dot, then the name of the constant.
The
variable types within parentheses describe the required parameters for
the methods.
Here is a list of some JFrame methods (there are many more – see the API1 if interested).
1
See the what? The java API (Application Programming Interface): a massive documentation of all the classes in
the java runtime environment. Have a look! Go to java.sun.com, look for the Resources menu, click on APIs, and
then select the most recent version (Java SE 6 as of this writing).
3|©Joshua Britton
setLocation (int, int)
setVisible (boolean)
setResizable (boolean)
setTitle (String)
setSize (int, int)
setDefaultCloseOperation (int)
ASSIGNMENT
FirstForm
description: Create a JFrame that is 500 pixels wide and 400 pixels high. Give it a title. Make it
impossible to resize. Place the JFrame’s top left corner 100 pixels to the right of the top left corner of
the screen.
4|©Joshua Britton
Download