Skip to main content

How to have a Center Screen Window in Java

Language


Some of the programs in java is set to its user location using the setLocation method of the JFrame or JWindow because they find it hard to center their window screen. So here in this tutorial, we will create a program that has a center screen window in Java.

Now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of centerScreen.java.

2. Import the following package library:

  1. import java.awt.*; //used to access the Dimension and Toolkit class
  2. import javax.swing.*; //used to access the JFrame class

3. We will initialize variables in our Main, variable frame as JFrame, kit as ToolKit, and wndSize as Dimension.

  1. JFrame frame = new JFrame("This is a Center Screen Window");
  2. Toolkit kit = frame.getToolkit(); // Get the window toolkit
  3. Dimension wndSize = kit.getScreenSize(); // Get screen size

4. Now, we will set the position to screen center & size to half screen size. We will use the setBounds method here of the frame and then get in on the Dimension class variable declared above.

  1. frame.setBounds(wndSize.width / 4, wndSize.height / 4, // Position
  2. wndSize.width / 2, wndSize.height / 2); // Size

5. Display the window using the setVisible method of the frame that returns to true.

  1. frame.setVisible(true); // Display the window

Lastly, close the Operation.

  1. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Output:

output


Here's the full code of this tutorial:

  1. import java.awt.*; //used to access the Dimension and Toolkit class
  2. import javax.swing.*; //used to access the JFrame class
  3.  
  4. public class centerScreen {
  5. public static void main(String[] args) {
  6. JFrame frame = new JFrame("This is a Center Screen Window");
  7. Toolkit kit = frame.getToolkit(); // Get the window toolkit
  8. Dimension wndSize = kit.getScreenSize(); // Get screen size
  9.  
  10. // Set the position to screen center & size to half screen size
  11. frame.setBounds(wndSize.width / 4, wndSize.height / 4, // Position
  12. wndSize.width / 2, wndSize.height / 2); // Size
  13. frame.setVisible(true); // Display the window
  14. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. }
  16. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.