It's good programming style to group sets of similar commands together in functions. It makes the program more readable, and if you want to run the same set of instructions again, all you have to do is run the function. With this in mind, I'm grouping all the Java code that deals with creating the window into one function.
Type in the createWindow function definition:
private static void createWindow() {
}
All the code to create the window goes between the function’s curly brackets. This way, anytime the createWindow function is called, the Java application will create and display a window.
Now, let's look at creating the window using a JFrame object. Type in the following code, remember to place it between the curly brackets of the createWindow function:
//Create and set up the window.
JFrame frame = new JFrame("Simple GUI");
What this line does is create a new instance of a JFrame object called "frame". You can think of "frame" as the window for our Java application.
The JFrame class will do most of the work of creating the window for us. It handles the complex part of telling the computer how to draw the window to the screen, and leaves us the fun part of deciding how it's going to look. We can do this by setting its attributes, e.g. how it looks, its size, what it contains, etc.For starters, let's make sure that when the window is closed, the application also stops. Type in:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
The JFrame.EXIT_ON_CLOSE constant sets our Java application to terminate when the window is closed.


