Sunday, March 29, 2009

Monday March 30: a null layout for the whole program? Sort of....

OK, our next trick will involve a little bit of skullduggery from the old AWT library: just because we're suing Swing doesn't mean we have to forswear the old ways entirely. Here is a way to ensure that your program starts up at center screen, no matter what it's size:

//make a Toolkit object
Toolkit TK = getToolkit();
//use toolkit object to find screen size
Dimension size = TK.getScreenSize();

//subtract 1/2 the width of JFrame from 1/2 the width of screen
//do the same for height
setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);

The Toolkit is actually part of the AWT libraries, but as you can see from this example, it can be used even with swing based programs. It has its best use when creating a Splash screen: a window controlled by a Timer bearing some sort of information about the program. It lasts for about 5 to 10 seconds, then vanishes to be replaced by a Frame , or JFrame in this case


OK, onwards to see if yuo can handle a VERY small leap!

Uncle Paulie

No comments:

Post a Comment