Chapter 17: Windows, Frames, Dialogs and Menus
On page 406 I suggested using the applet's bounds() method to make an educated guess about where to pop up a Frame relative to the monitor. You con do this much more accurately and directly with the getScreenSize() method of java.awt.Toolkit. This returns a Dimension object that tells you how wide and tall the screen is in pixels. For example, here's an applet that centers a Frame on the monitor:
import java.applet.Applet;
import java.awt.Toolkit;
import java.awt.Frame;
import java.awt.Dimension;
public class centerFrame extends Applet {
public void init() {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension ss = tk.getScreenSize();
int fwidth = 320;
int fheight = 240;
Frame f = new Frame("My Window");
f.resize(fwidth, fheight);
f.move((ss.width - fwidth)/2, (ss.height - fheight)/2);
f.show();
}
}
Examples from Other Chapters
[ Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]
Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified October 5, 1996