A Graphical HelloWorld

Fire up your favourite editor and create the following code in a file called HelloAp.java. Remember as ever, Java is utterly case sensitive.

/* HelloAp */
// A Single line comment
import javax.swing.*;

public class HelloAp extends JFrame{
    public static void main(String argv[]){
	HelloAp helloap = new HelloAp();
	helloap.go();
    }
    public void go(){
	JButton button = new JButton("Hello");
	getContentPane().add(button);
	setSize(100,100);	
		
	setVisible(true);

    }
}

Compile this code by typing

javac HelloAp.java

You are now ready to test your first graphical Java program. From the command prompt type

java HelloAp

This is a screen shot from this code running under Linux, the result under Windows should be similar.

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