You will want to get an idea of what code is generated by NetBeans so that you can add functionality to your applet. Go to the window that contains the design pad that you used to construct your applet. At the top of the design pad is a menu bar. Click on the “Source” button to see the generated code.
You will notice that several lines are grayed out. Most of these are comments that are not executed by the compiler. Comments are formed in two ways.
\*
Second line of comments
Third line of comments
Fourth line of comments
*/
//First line of comments
//Second line of comments
//Third line of comments
Much of the code that you see is contained in a blue background. This is automatically generated by NetBeans and cannot be altered by the user.
The first meaningful line in this code is
package edu.usafa.helloworld;
This contains the details of the location of this file, and perhaps others. You should not alter this unless you have a really good reason to do so. When using multiple packages and files, you may need to tell the java compiler where things are located. If so, you will add a line after the package declaration that may look like:
import com.jimrolf.calcTool.CalcTool1;
This line says “look in the package com.jimrolf.calcTool and import the CalcTool1.class file for use in this program.” For today’s workshop, this is unnecessary.
Every java program must contain at least one class. A class is a collection of methods (i.e. subroutines) and objects (variables, buttons, panels, labels, text fields etc.) that work together to provide functionality. The class structure is what makes java object-oriented and easily extensible. Consequently, we will try to take advantage of this structure.
Our class statement is
public class HelloWorldApplet extends javax.swing.JApplet {
//code goes here
}
The “public” attribute describes who can have access to things in the class. “public” allows other classes to access members of this class. “private” access allows no other classes to access members of the class, while “protected” allows members of the same package to access class members.
HelloWorldApplet is an extension of javax.swing.JApplet. As such, it inherits all the properties, methods (i.e. subroutines) and objects of a JApplet. There are other kinds of applets, namely of the java.awt.Applet variety. I recommend using swing components (i.e. javax.swing.JApplet, javax.swing.JPanel, etc.) because they have a more consistent look-and-feel across different platforms. These objects also typically have more properties which gives the programmer more control over how they operate.
Also, you should note the opening and closing braces, {} which are required to contain the methods and objects for this class.
Typically, one class is declared in a file of the same name. You can search in the src folder of your project and you will find the file HelloWorldApplet.java that contains this code.
Scroll down to the bottom of the page and you will see some variables declared in a blue background (i.e. you can’t change these).
private javax.swing.JPanel backgroundPanel;
private javax.swing.JLabel helloWorldLabel;
Note that each line ends in a semicolon. Most lines in java will utilize this convention.
The variable name for the first variable is backgroundPanel, its type is javax.swing.JPanel (or JPanel for short) and its scope is “private.”
In general, variable declarations will be of the form
scope type variableName;
scope defines the visibility of the variable as well as how long it lives in the program’s memory. The “private” designation is the most restrictive. It means that nothing outside of the HelloWorldApplet class can access these kinds of variables. “Protected” means that other classes in the same package can access the variable, as well as subclasses of the current class . “Public” means that any other classes can access the variable. It is good programming practice to choose the most restrictive scope for variables unless you have a good reason not to. This means that the “private” designation is probably the one you will use most often. Finally if no scope is given , the scope defaults to the current class and other classes within the package.
type refers to the type of object the variable will contain. Other types include int
(integers), double
(double precision real numbers), boolean
(variable with true/false values), and String (strings of text). There are several other primitive types defined by java, as well as a myriad of user-defined types. Go here for more other types.
Every applet must have an init method (i.e. subroutine).
public class HelloWorldApplet extends javax.swing.JApplet {
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}//your code goes here
}}
This method is the first method executed after the applet is loaded into the browser, so it is used to initialize things you want started up at the beginning. Of course you want to visualize the objects such as buttons, labels, etc. that you’ve place on the object, so these are the kinds of things that are initialized here.
NetBeans uses a try/catch statement and the
If you scroll down, you should see the definition of the initComponents() method contained in a blue background.
private void initComponents() {
backgroundPanel = new javax.swing.JPanel();
helloWorldLabel = new javax.swing.JLabel();getContentPane().setLayout(null);
backgroundPanel.setLayout(null);
backgroundPanel.setBackground(java.awt.Color.white);
helloWorldLabel.setText("Hello World");
backgroundPanel.add(helloWorldLabel);
helloWorldLabel.setBounds(130, 90, 100, 20);getContentPane().add(backgroundPanel);
backgroundPanel.setBounds(0, 0, 400, 300);}
Here is where each object is instantiated (i.e. created) and where specific information regarding the size, etc. of each object is declared.