To create a JFrame project in Netbeans 6.x do the following:
- Start Netbeans
- Close any projects previously left open.
- Under the File menu item, select New
Project
- This will bring up the New Project Window, set the
following
- Category - Java
- Projects - Java Application (not Java Desktop
Application)
- press the Next button
- The name and location window will now be display, set the
following
- Project Name - use whatever you like (don't use spaces
though), e.g . JavaApplication1
- Project Location - browse to an area on your harddisk, floppy
(not recommended) or thumb drive where the project folder is to be
created (folder will be created using the project name).
- Leave on the Set as Main Project
box
- Turn off the Create Main Class box,
if you leave it on, it will automatically create a Main.java file
- Press Finish
- you will now have an empty project created.
To create a Java Application within your project do the
following:
- From the File menu item, select New
File
- This will bring up the New File Window, set the following
- Categories - select Swing GUI Forms
- File Types - select JFrame Form
- The new JFrame Form window will now be displayed, set the
following
- Fill in the class name, e.g. Demo1. The first
letter of each word in the name should be capitalized, there should be
no spaces in the class name.
- Optionally, fill in the package name, e.g.
application1. Package names should be in all lower
case.
- Your program should look something like the picture below
Completing your first application
- At this point you have created a JFrame (windows) application
that has a blank screen. To add something to it, use the
following procedure:
-
- Right click on the JFrame in the "Inspector window", select
the "Set Layout" option and choose null layout.
- To add a label to your JFrame
- Click on the Label (JLabel) item in the Palette window
- Click again in the design area
- Double click on the JLabel
- Add a button to your JFrame
- Click on the JButton item in the Palette window
- Click again in the design area (below your JLabel)
- Right click on the Button you just added and select "Edit
Text" to change the text to "Exit"
- Stretch out the JButton so that all the text appears
- Right click on the JButton and select "Change variable
name", then type in exitButton.
- Make your Exit JButton do something
- Double click on "exitButton" in the Inspector window or
on the Exit Button in the design window.
- This will cause the "source" tab to be displayed in the
work area
- Just below the line that reads "// TODO add your
handling code here:" type in System.exit(0);
private void
exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
- Run your program,
- Press on the run button in the tool bar (this button has a
green triangle with a yellow folder behind it).
- If all went well you should see the final image shown below.
- Other things you can do:
- Change the fonts and colors of the items in your JPanel
- Right click on the item, then select properties.
- To change the fonts, then press the little square button
with the three dots next to the font item.
- To change the color, then press the little square button
with the three dots next to the foreground item.