Java Reference
In-Depth Information
ing the screen size in pixels. You can use the getToolkit() method to help set the size and position of a
window on the screen. You can modify the previous example to demonstrate this.
TRY IT OUT: Sizing Windows with Toolkit
You use the Toolkit object to display the window in the center of the screen (or primary monitor if you
have more than one), with the width and height set as half of the screen width and height:
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Toolkit;
import java.awt.Dimension;
public class TryWindow2 {
public static void createWindow(){
JFrame aWindow = new JFrame("This is the Window Title");
Toolkit theKit = aWindow.getToolkit();
// Get the
window toolkit
Dimension wndSize = theKit.getScreenSize();
// Get
screen size
// Set the position to screen center & size to half screen size
aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
// Display
the window
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
If you try this example, you should see the application window centered on your primary display with
a width and height of half that of the screen. Of course, you could set the window to any position and
dimensions you like.
How It Works
Search WWH ::

Custom Search