Google
 
Site navigation: [ Home | Theory | Java | Moodle courses | Resource wiki | About ]

Applications

All about

Java Applications

 

 

An application starts with a main method of this form.

This main method just calls the Constructor for the Class.

The Constructor contains the executable code.

This outputs to "the console" in text only mode.

 

 

Note: You need to run these applications in BlueJ by right-clicking the Class and selecting main() to get it started.

A Frame is like a windows Window

 

 

 

 

 

If we don't set size and make visible, we won't see anything.

 

 

 

 

 

 

 

An inner class is a class that is used or defined in another class and they are convenient when writing event-driven applications.

Here we want to implement a window listener that detects only the window closing event.  The easiest way to do this is to extend the convenience class WindowAdapter. 

However, we are already extending Frame so we can't do that.  We could implement the WindowListener interface but then we have to include code for all 7 of the window events (such as windowIconified (minimized), windowDeicoconified (restored), etc).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

On this page: [ Outline | Inner Classes | Main Methods | Summary ]

Outline of an application

/**
* Example application
*
* @author Mr J and programmers everywhere
* @version 999999999.99
*/

public class Application
{
   public static void main(String[] args)
  {
     new Application();
   }
  
/**
   * Constructor for objects of class Application
   */

   public Application()
   {
     System.out.print("Hello World!");
   }
}

If we want to use a GUI ( Graphical User Interface ) we have to use the awt as in our Applets.

import java.awt.*;
/**
* Example application
*
* @author Mr J
* @version 9
*/

public class GUIapplication extends Frame
{
   Label message = new Label("Hello World!");

   public static void main(String[] args)
   {
     new GUIapplication();
   }
   /**
   * Constructor for objects of class Application
   */

   public GUIapplication()
   {
     add(message);
     setSize(100,100);
     setVisible(true);
   }
}

Back to top

Inner classes

There is a slight disadvantage of this Application - it won't close (although it will in BlueJ if you re-compile the code). The easiest way to handle this is to use an inner class (we met these briefly in VideoTapeInner )

import java.awt.*;
import java.awt.event.*;
/**
* Example application
*
* @author Mr J
* @version 9
*/

public class GUIapplicationWindow extends Frame
{
   Label message = new Label("Hello World!");

   public static void main(String[] args)
   {
     new GUIapplicationWindow();
   }
   /**
   * Constructor for objects of class Application
   */

   public GUIapplicationWindow()
   {
     // inner class to intercept window events
     addWindowListener ( new WindowAdapter()
     {
       // we are only interested in window closing
       public void windowClosing(WindowEvent e)
       {
         // Exit the system, 0 is a normal exit code
         System.exit(0);
       }
     } );
     add(message);
     setSize(100,100);
     setVisible(true);
   }
}

Back to top

Summary

In summary:

  • Applications can access files on the host computer
  • Applications do not run in a web page
  • Applications are contained within a Frame
  • Applications have a main method to start them up
  • Inner Classes are a convenient way to deal with Window Events

On to the Biscuit Collection Application.

Related: [ Java home | previous: applications | next: The Biscuit App ]

So far we have worked exclusively with Applets, small applications designed to run in a web page. Since Java Applets are designed to be hosted on a user's computer, Applets are not allowed to access the local filing system to create and edit files.

Therefore our file handling programs must be applications rather than Applets.  For this reason you won't find demonstration programs on these web pages for applications. 

However, the source code is provided so that you can simply import or cut and paste the listings.


 
The site is partly financed by advertising revenue, partly by online teaching activities and partly by donations. If you or your organisation feel these resouces have been useful to you, please consider a donation, $9.95 is suggested. Please report any issues with the site, such as broken links, via the feedback page, thanks.

Questions or problems related to this web site should be addressed to Richard Jones who asserts his right to be identified as the author and owner of these materials - unless otherwise indicated. Please feel free to use the material presented here and to create links to it for non-commercial purposes; an acknowledgement of the source is required by the Creative Commons licence. Use of materials from this site is conditional upon your having read the additional terms of use on the about page and the Creative Commons Licence. View privacy policy.

Creative Commons License


This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. © 2001 - 2009 Richard Jones, PO BOX 246, Cambridge, New Zealand;
This page was last modified: May 31, 2009