Java Reference
In-Depth Information
Table 14.5
Useful Methods of
Dimension
Objects
public Dimension(int width, int height)
Constructs a
Dimension
representing the given size
public int getWidth()
Returns the width represented by this
Dimension
public int getHeight()
Returns the height represented by this
Dimension
Dimension
class, such as how to construct it. Table 14.5 lists the methods with
which you should be familiar.
The new version of the
SimpleFrame
program that follows creates a frame and
sets several of the properties listed in Table 14.4. In this version, we give the frame a
color, set its location and size on the screen, and place text in its title bar. We also set
the “default close operation” of the frame, telling it to shut down our Java program
when the frame is closed:
1 // Sets several properties of a window frame.
2
3
import
java.awt.*; // for Dimension
4
import
javax.swing.*; // for GUI components
5
6
public class
SimpleFrame2 {
7
public static void
main(String[] args) {
8 JFrame frame =
new
JFrame();
9 frame.setForeground(Color.WHITE);
10 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11 frame.setLocation(
new
Point(10, 50));
12 frame.setSize(
new
Dimension(300, 120));
13 frame.setTitle("A frame");
14 frame.setVisible(
true
);
15 }
16 }
When the program is run, the following window appears:
When the window is closed, the program exits.
Search WWH ::
Custom Search