Line by line analysis of the graphical HelloWorld

/* HelloAp */ // A Single line comment 

These two lines are comments, text put into the program to explain the code not to actually change how the program works. The first line using the /* version of comments means the comment will continue until it is closed by a matching */ set of characters. The second version // is a comment that stops at the end of the line.

import javax.swing.*; 

These lines tell the compiler that you will be using these classes. Although this can seem a bit like the use of the


pre-processor directive in C/C++, it is actually more like the DOS path statement. If that doesn't mean anything to you don't worry right now. The import directive statement is used to integrate Java classes already in existence. In this case


is a reference to the swing classes. This contains the basic Graphic components such as buttons, frames, labels, etc etc.

public class HelloAp extends JFrame{ 

This line is similar to the line in the stand alone code that declared the HelloWorld class. The important difference is the part that states

extends JFrame 

The keyword extends means that this class will extend an already existing class. This actually means

extends javax.swing.JFrame 

But because of the import statement at the start of the program you do not need to specify the javax.swing part of the JFrame class name. This is part of the Java inheritance mechanism. By extending the Frame class this new class will obtain the functionality of the Frame class such as the ability to add components and repaint itself.

The main method has a line to create a new instance of the HelloAp class, this is because the main method is static and we need a new instance from which to call instance methods. Once an instance has been created the go method is called to start the program running. The choice of the method name go is entirely arbitrary.

The go method starts by creating an instance of the Jbutton class, which as you can guess from its name acts like a button, ie it can be clicked and can have a label indicating what it is to be used for. In this example we don't actually program the button to do anything.

JButton button = new JButton("Hello"); 

This line is to create a new instance of the Button class. Note that the program knows about the Button class because of the line

import javax.swing.*; 

at the start of the code. The asterix (*) means make all of the classes from the Swing classes available. For the purposes of this program it would also be acceptable to have the import declarations as

import javax.swing.JFrame; import javax.swing.JButton; 

The JButton has been sent the String "Hello World" as part of its constructor (initialisation code).

The next line reads


The important bit of this line is the call to the add method. This adds the button to the current class instance. The last two lines set the size of the frame and then ask it to display itself. Note that the final closing brace is not terminated with a semi colon.

This graphical HelloAp is the basic structure of most Java graphical applications. Most applications involve creating a Frame, adding components such as menus, buttons, labels and text entry fields and then processing the events that happen when the user uses the mouse or enters data.

Last modified: Thursday, 24 July 2014, 2:54 PM