edt

Java JFrame example: How to display a JFrame

This morning when I saw some Java JFrame code on a mailing list, it made me think that I needed to put a simple JFrame example out here, something that would show how to properly construct and display a JFrame without getting into a discussion of anything else. Here are two examples that show the correct technique.

How to center a JFrame on screen

I'm often asked, "How do I center a JFrame on screen?", or, "Is there a method to center a JFrame?"

I always center my initial JFrame on screen using the setLocationRelativeTo method, and pass it a null reference. As the Javadoc for setLocationRelativeTo method states:

Java JFrame: How to create, center, and display a JFrame alvin June 7, 2009 - 1:46pm

Java JFrame FAQ: How do I properly create and display a JFrame? While you're at it, how do I center a JFrame?

In this Java tutorial I'll demonstrate how to create and display a JFrame Other than the comments in the source code, I'm going to keep the code as simple as possible, so I can demonstrate how this works.