How to have a Center Screen Window in Java

Submitted by: 
Language: 
Visitors have accessed this post 617 times.

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 Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.