Java Applications, how to develop a java application and how to start developing java? is the the content of this topic ,this topic covers Java Applications. Creating Java applications is rather easy, once you know how it works.
A tiny Java application
The basic layout of a
Java application is similar to that of a
C# program. The Hello World program in
Java looks like this:
java code
public class StartJava
{
public static void main(string args[])
{
System.out.println("My First Java Application");
}
}
This sample program is not really difficult to understand. It defines a class called Hello World and has a method called main which most people may know from C or C++ programming. Within the main function we use the
println() function to write the message "Hello World" on the screen. The
println() function is similar to
printf in C.
Note: when compiling Java applications make sure that your file has the name as your main class. If it does not, the java compiler will complain an error.
Windows applications with JavaWriting windows applications in Java is easy. This is because Java is a 100 % object oriented programming language and you don't need to worry about API function calls or pointers or unsafe castings.
java code
import java.awt*;
class myFrame extends Frame
{
//Class Constructor
myFrame()
{
//create a new instance of WriteText
add("Center", new WriteText());
//our main window Listener
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
//the main function of this class
public static void main(string args[])
{
new myFrame ();
}
}
// Class to draw on Canvas
class WriteText extends Canvas
{
public void paint(Graphics g)
{
// Write strings
g.drawString("My First Frame",100,100);
}
}
Whoops! Did i say it was easy? Well, it actually is easy once you understand how it works. When writing windows applications with Java, you basically need 2 classes. The first class implements the application body and
Eventlisteners. The second implements the application logic.
The AWT framework To be able to use the windows application programming features of Java you first need to import the
java.awt.*; classes. There is a new newer GUI package is now in use which is
javax.swing.*;. Swing the newest Java GUI API and the most widely use to the time of posting this topic.
The Java EventListener model All windows applications written in Java need to implement so called Listeners. The class framework of Java supports various forms of Listeners. Wind
owListeners, MouseListeners, KeyboadListeners,.. Hope it helps.