Creating a GUI applet (i.e. one that you can see) in NetBeans requires several steps. Thie nice thing about NetBeans is that it will automatically generate a lot of code that otherwise would require a lot of prior knowledge.
Add a GUI applet form to your project.
This form is what will allow you to drag-and-drop items onto a canvas and quickly build GUIs without having to create all of the underlying code (a very tedious process).
- Highlight edu.usafa.helloworld and choose File/New File/Java GUI Form/JApplet Form. I gave mine the name “HelloWorldApplet.” Convention dictates that all Java classes should begin with a capital letter. Lots of things should pop up when you click the “Finish” button, including a Design pad. You can grab the corners of the applet to make it the size that you want. For now, the default size should suffice.
- Set the layout manager. The layout manager controls the placement of different objects when the applet is re-sized by a browser. The default layout is “Free Design” which is a very nice layout manager. However, I use “Null Layout.” This gives me the most control over where objects wind up on the applet. It also takes a bit more time to finely control their placement. Right click on the applet form and choose “Set Layout/Null Layout.”
Add a JPanel to your applet.
The JApplet is primarily used as a container for other objects. So we will add a JPanel to our applet. This will allow us fine control over properties such as the background color, the foreground color, the default font size and type, etc.
- Choose JPanel from the “Palette” menu. You can grab this object and drag it onto the designer pad.
- Change the background color. The properties panel on the lower right hand side of the IDE gives you the ability to change the background color, size, etc. Click on “background” and change it to whatever color you like. I’m going to change mine to white.
- Change the size of the JPanel to match the width and height of the underlying applet container. If you haven’t changed the original applet size, the default is 400x300. To check, click on the applet form. Go to the properties panel (lower right corner) and click on “Code.” The Designer Size will tell you the size of the applet. Go back and change the Layout dimensions to a Width of 400 and a Height of 300. Additionally, you will most likely want X to be 0 and Y to be 0. This is the location of the upper left corner of the JPanel. Note: The x axis is positive as you move to the right. The y axis is positive as you move down. The origin is assumed to be in the upper left corner.
- Change the name of the JPanel. Right-click on the JPanel and choose “Change variable name.” The default name is jPanel1. I’m going to change the name to
backgroundPanel
.
- Change the layout manager to “null.” Note: This is not absolutely necessary and is a personal preference.
Add a JLabel to backgroundPanel.
- Drag and drop the JLabel component from the Palette panel.
- Resize as you wish. You can use the mouse to grab a corner or utilize the Properties panel to do this.
- Double click inside the label. Replace “jLabel1” with “Hello World.” Note: You can accomplish the same thing by changing the “text” property in the Properties panel.
- Change another property or two. For example, you might trying changing the font, background color, and/or foreground color.
- Change the name of the label to
helloWorldLabel
Build and Run the applet.
- From the menu bar, choose Build/Build Main Project. You may get asked to designate your main project. Go ahead and designate HelloWorldProject as the main project. The Output window will contain any error messages. If everything compiles successfully, you will see BUILD SUCCESSFUL.
- In the Project panel, right click on HelloWorld.java and choose “Run file.”
- If everything is working properly, a window should pop up containing your applet!