![]() |
Java Application Tutorial #1
Many (if not all) tutorials seem to begin with an extensive look at the language in question with examples in text mode and will only touch on the GUI (Graphical User Interface) aspects towards the end. It is not actually necessary to understand the language in depth in order to be able to build GUI enabled programs which is the bit that most people really want to get into (well I do anyway). Hence, I will take you through building Windows style applications at the very beginning and teaching the language as we go along. This means a slightly steeper learning curve at the beginning but will I hope be more interesting than other tutorials you might have come across. Incidentally, if you want to build applets that appear in web pages then you've come to the wrong tutorial. Please choose the Applet tutorial from the main page. On with the show then. Here is the source code for a very simple application.
public class TestMain
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
It is the ubiquitous Hello World program and prints a line of text only. Hey, what about the GUI bits I promised? Well, this is the only non-GUI program and it is necessary just to show how an application (rather than an applet) is constructed. Those of you that are familiar with the C programming language will recognise the word main although it is in an unfamiliar context. Note also that this is not a program but a class (just as a program written in C is actually a function). Java is also very strict on the case (capitals are upper case) particularly when it comes to compiling it which we'll do now. The easiest way is to create two batch files the first of which will compile the code and I have called j.bat which you can create using Notepad in the normal way and should contain the following text.
c:\java\bin\javac TestMain.java
Make sure that the filename matches EXACTLY the
case of the class name and ends in .java. Cut
and paste the above source code into notepad and save it as TestMain.java
in a new folder called TestMain and then double-click
on the TestMain.bat file in Windows Explorer and
a DOS box will popup and the code will (hopefully) compile. If
there is an error then it will show in the window and highlight
the line and position of the error. If there is an error saying
that the java source code file cannot be found
then the case of either the program or the batch file do not
match. All being well, a file called TestMain.class
will have been created in the TestMain directory. You may have to
adapt the line in j.bat if java has been installed in a different
directory. If yoiu get a 'deprecated api' error
then you are using a later version of Java but it should compile
anyway. Close the DOS box (if it hasn't already disappeared). Now
to run the compiled code. Create another batch file in the
TestMain folder called jrun.bat and insert the
following code:-
c:\java\bin\java TestMain
Note that it isn't necessary to add .class to
the file name. Double-click on the jrun.bat file
and another DOS box will appear hopefully with the words Hello
World! somewhere in it. Congratulations, you have just
written your first Java application!
To summarise the steps.
Now we'll go through the code a line at a time. Bear with me, it is relevent and will help you understand what is to follow when we get to the more interesting stuff.
public class TestMain
{
This is a public class (as opposed to a private one) which is accessible to other classes and we have called it TestMain. Now to the nitty gritty. What is a class? Classes are essential to your understanding of Java and you cannot do without them. A class is an object. Now don't break out in a sweat. An object is merely a piece of code that contains both code (called methods) and data. That's it! Not too bad was it? And classes are the basis for Object Orientated Programming (or OOP for short). Back to the code. The bracket then tells java that there is a block of code to come.
public static void main(String args[])
{
Every java application (NOT applet) must have a main class so here it is. The args[] takes a parameter from the command line that starts the program - don't worry about it at this stage. The opening bracket tells java that a block of code is to follow.
System.out.println("Hello World!");
}
}
This is where the real work is done. A simple line of text is written to the console (DOS) window. This useful for debugging because there is no alert or msgbox function as provided by languages like Javascript (unless you write your own or upgrade to version 1.2 - stay tuned). And then the blocks of code are closed using closing braces. Note the use of indentation of the lines so we can see which code belongs to a particular block of code and is an essential part of writing good java code. So now we are ready to get stuck into a GUI program?
import java.awt.*;
public class TestApp
{
public static void main(String args[])
{
myFrame Window;
Window=new myFrame("Untitled");
Window.show();
}
}
class myFrame extends Frame
{
private TextArea myTextArea=new TextArea(" ",25,80);
myFrame(String Title)
{
super(Title);
setLayout(new GridLayout(1,1));
this.add(myTextArea);
}
public boolean handleEvent(Event ev)
{
if (ev.id == Event.WINDOW_DESTROY)
{
this.dispose();
System.exit(0);
return true;
}
return super.handleEvent(ev);
}
}
Don't worry too much about the code just save it in a file and call it TestFrame.java and place it in a new folder called TestFrame. Copy the batch files previously created and change the names being run and compiled to TestApp and TestApp.java respectively. Compile and the run the program. Note that the window is very small although it can be resized or maximised to reveal the beginnings of a very simple test editor. I'll go into the inner workings of the code in the next tutorial.