NetBeans is using javax.swing.JFrame
as a base for all Swing applications; in this recipe, we will use the word Frame to denote instances of javax.swing.JFrame
class.
The Frame is nothing more than a border where the developer will place the components; think of a real frame where the canvas goes, so the artists can paint on it, and where other components will be added. The provided implementation from NetBeans is very basic in its content but it already has some basic controls, so the designer can close, expand, and align the components implemented.
Desktop Applications will have at least one Frame, but several other components can be included in its "body".
For this recipe, you will require a Java Desktop Project created according to the recipe Creating a Java Desktop Application recipe, given at the beginning of this chapter.
With the IDE and the Java Desktop Application project:
An empty JFrame will be shown on the editor.
The IDE creates the MyJFrame.java
class and places it under the desktopapp
package. NetBeans then automatically generates the code for the default close operation, adds and configures a javax.swing.GroupLayout
, and names it as "Form".
The execution behavior is also added by the IDE with a main method added to the body of the class, different from what was previously seen in Creating a Java Desktop Application.
So this is more of a "fresh start" for the entire application. No window, no status bar, and no other components. It is a clean sheet where the developers have total liberty to do what they want.
Want to deploy your Swing application on a different platform from the one where it is being written? Want to check out what code was generated by NetBeans in the background? Your application does not need to close when pressing the x (close) button on the corner of the window.
Swing gives you the possibility of changing how your applications Look and Feel. The Look and Feel of an application is basically how the window border, buttons, titles, and system colors of a program are specified.
Swing has different Look and Feel for various operating systems, so your application will look different when launched under Windows, Linux, or Solaris.
To change the Look and Feel of you application:
If you wish to check the look and feel of a configuration before setting it up as the default, then open the MyJFrame.java
class and on the GUI Designer, click on Design and the Inspector. A window will appear on the left corner of the IDE.
Then right-click on the JFrame icon, select Preview Design and click on one of the three options given by the IDE: Metal, CDE/Motif, or GTK+.
On clicking the desired choice, the IDE will then launch the application with the modified Look and Feel.
NetBeans also gives users the option of downloading different Look and Feel files. Please visit the following links for more information:
One of the main benefits in using the GUI Design is how practical and trivial it is to start your own application. If we check the contents of the files created by NetBeans, we can see how much heavy lifting the IDE does for us.
To check some of that heavy lifting, on the GUI Designer, click on Source; the IDE will then show the contents of MyJFrame.java
.
Inside the editor, the code generated by the IDE is "hidden" from the user with an IDE configuration parameter.
Expand the Generated Code and then the initComponents
method to see the contents.
With the MyJFrame.java
open and Design button toggled, check the properties window. The Properties button under Properties needs to be toggled.
By clicking on the right-hand side of defaultCloseOperation, you'll get a drop-down menu with the following options:
System.exit(0)
.3.138.61.133