Answers

Question and Answer:

  Home  Java GUI Framework

⟩ How to get the screen size? How Do I center a window on the screen?

Manually, pre 1.4:

Use java.awt.Toolkit.getDefaultToolkit().getScreenSize() to get the screen size, and do the math:

import java.awt.*;

Dimension winSize = win.getSize();

Dimension screenSize =

Toolkit.getDefaultToolkit().getScreenSize();

win.setLocation(

screenSize.width / 2 - winSize.width / 2,

screenSize.height / 2 - winSize.height / 2

);

Since 1.4:

Window.setLocationRelativeTo(null);

 147 views

More Questions for you: