The JFrame class itself has only a few methods for changing how frames look. Of course, through the magic of inheritance, most of the methods for working with the size and position of a frame come from the various superclasses of JFrame. Among the most important methods are the following ones:

illustrates the inheritance chain for the JFrame class.

Inheritance hierarchy for the JFrame and JPanel classes

Java graphics 07fig05

As the API notes indicate, the Component class (which is the ancestor of all GUI objects) and the Window class (which is the Frame's class parent) are where you need to look to find the methods to resize and reshape frames. For example, the show method that you use to display the frame lives in the Window class. As another example, the setLocation method in the Component class is one way to reposition a component. If you make the call

setLocation(x, y)

the top-left corner is located x pixels across and y pixels down, where (0, 0) is the top-left corner of the screen. Similarly, the setBounds method in Component lets you resize and relocate a component (in particular, a JFrame) in one step, as

setBounds(x, y, width, height)

Java graphics notes_icon

For a frame, the coordinates of the setLocation and setBounds are taken relative to the whole screen. As you will see in , for other components inside a container, the measurements are taken relative to the container.Remember: if you don't explicitly size a frame, all frames will default to being 0 by 0 pixels. To keep our example programs simple, we resize the frames to a size that we hope works acceptably on most displays. However, in a professional app, you should check the resolution of the user's screen and write code that resizes the frames accordingly: a window that looks nice on a laptop screen will look like a postage stamp on a high-resolution screen. As you will soon see, you can obtain the screen dimensions in pixels on the user's system. You can then use this information to compute the optimal window size for your program.Java graphics exclamatory_icon

The API notes for this section give what we think are the most important methods for giving frames the proper look and feel. Some of these methods are defined in the JFrame class. Others come from the various parent classes of JFrame. At some point, you may need to search the API docs to see if there are methods for some special purpose. Unfortunately, that is a bit tedious to do with the SDK documentation. For subclasses, the API docs only explain overridden methods. For example, show is applicable to objects of type JFrame, but because it is simply inherited from the Window class, the JFrame documentation doesn't explain it. If you feel that there should be a method to do something and it isn't explained in the documentation for the class you are working with, try looking at the API docs for the methods of the superclasses of that class. The top of each API page has hyperlinks to the superclasses, and there is a list of inherited methods below the method summary for the new and overridden methods.To give you an idea of what you can do with a window, we end this section by showing you a sample program that positions one of our closable frames so that:

For example, if the screen was 800 x 600 pixels, we need a frame that is 400 x 300 pixels and we need to move it so the top left-hand corner is at (200,150). To find out the screen size, use the following steps. Call the static getDefaultToolkit method of the Toolkit class to get a Toolkit object. (The Toolkit class is a dumping ground for a variety of methods that interface with the native windowing system.) Then call the getScreenSize method that returns the screen size as a Dimension object. A Dimension object d simultaneously stores a width and a height, in public (!) instance variables width and height. Here is the code:

Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;

We also supply an icon. Because the representation of images is also system dependent, we again use the toolkit to load an image. Then, we set the image as the icon for the frame.

Image img = kit.getImage("icon-java-image.gif");
setIconImage(img);

Depending on your operating system, you can see the icon in various places. For example, in Windows, the icon is displayed in the top-left corner of the window, and you can see it in the list of active tasks when you press ALT+TAB. is the complete program. When you run the program, pay attention to the "Core Java" icon.Java graphics exclamatory_icon

It is quite common to set the main frame of a program to the maximum size. As of SDK 1.4, you can simply maximize a frame by calling

frame.setExtendedState(Frame.MAXIMIZED_BOTH);
Java graphics notes_icon

If you write an app that takes advantage of multiple display screens, you should use the GraphicsEnvironment and GraphicsDevice classes to find the dimensions of the display screens. As of SDK 1.4, the GraphicsDevice class also lets you execute your app in full-screen mode.

Example CenteredFrameTest.java

 1. import java.awt.*;
 2. import java.awt.event.*;
 3. import javax.swing.*;
 4.
 5. public class CenteredFrameTest
 6. {
 7. public static void main(String[] args)
 8. {
 9. CenteredFrame frame = new CenteredFrame();
10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11. frame.show();
12. }
13. }
14.
15. class CenteredFrame extends JFrame
16. {
17. public CenteredFrame()
18. {
19. // get screen dimensions
20.
21. Toolkit kit = Toolkit.getDefaultToolkit();
22. Dimension screenSize = kit.getScreenSize();
23. int screenHeight = screenSize.height;
24. int screenWidth = screenSize.width;
25.
26. // center frame in screen
27.
28. setSize(screenWidth / 2, screenHeight / 2);
29. setLocation(screenWidth / 4, screenHeight / 4);
30.
31. // set frame icon and title
32.
33. Image img = kit.getImage("icon-java-image.gif");
34. setIconImage(img);
35. setTitle("CenteredFrame");
36. }
37. }

java.awt.Component 1.0

Java graphics api_icon

java.awt.Window 1.0

Java graphics api_icon

java.awt.Frame 1.0

Java graphics api_icon