Java Notes
Example - First Window
![]() |
This is about the simplest GUI (Graphical User Interface) window that you can make. The window is very small, and will appear in the top left corner of the screen, so you may not see it at first. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
//--- basic_windows/FirstWindow.java - Creates a very small window! // This is just about the smallest GUI program you can write. // It does nothing, but you can see that the close box works // and the window can be resized. // Fred Swartz 2004-10-26 import javax.swing.*; ////////////////////////////////////////////////////// class FirstWindow class FirstWindow { // ===================================================== method main public static void main(String[] args) { JFrame window = new JFrame("My First Window"); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setVisible(true); } } |
Notes referencing line numbers
- Comments: Every source file must start with comments - for human readers, not the compiler. Include the name of the file, a short description, your name, and the date. More extensive descriptions are needed for larger programs.
- Imports - The
import
statement specifies library classes that are needed. This statement imports all classes from thejavax.swing
package, altho only theJFrame
class is used. - Class definition - The name of the source file must be identical to the class name including case, plus a .java extension.
- Main - The header for the main program must look like this.
JFrame
is the Java class for a "window". This statement does several things.- It declares a variable to reference the window we're about to create.
new
creates a new instance (object) of JFrame by calling its constructor.- The window title bar text is specified in JFrame constructor call.
JFrame window; // Declare local variable to hold JFrame. window = new JFrame(); // Create new JFrame and save it. window.setTitle("My First Window"); // Set the title bar text.
- Close -
JFrame
automatically implements basic elements of a window: resizing, minimizing, titlebar, etc, however by default a close box click only closes the window, but doesn't stop the program! CallingsetDefaultCloseOperation(...)
with this parameter causes a user click on the close box to stop the program in addition to making the window disappear. There is also a way to make a click on the close box run a method (function) so that the user can be prompted to save work, etc. - setVisible makes the window visible and starts a thread to monitor the GUI.
- Return from main - After the main program displays a window, it returns
and the GUI just sits there waiting for the user to do something.
This event-driven programming style is
different than the traditional read-compute-write loop.
A more realistic program has many controls (buttons, menus, etc)
that allow the user to initiate many possible actions from the
program. For example, when a button is clicked, a particular
method that you specify would be called.
Do not call
System.exit(0)
at the end of main. You want the GUI portion of the program to continue to run.