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

A simple GUI Applet

PressMe.Java

A starter Applet which uses the awt (abstract windows toolkit).

 

 

Explanations

statements which import special Applet Classes

Comments in blue are ignored by the compiler - the program that turns Java language to machine code.

PressMe is the name I chose for this Class.

These are graphical user interface (gui) objects imported with java.awt.*

 

An init method is needed by any subclass of the Applet Class - this Class is a subclass (it extends Applet), Applet is the superclass.

 

ActionListener is an interface implemented by this Class.

 

This method is required by the ActionListener interface. ActionEvent is a Class defined in the ActionListener interface.

setText and getText are methods of the Label and TextField Classes.

If you can't see an Applet here, download the JRE from http://www.java.com . It's free and easy to do. Otherwise cut and paste the code into your favourite editor.

Source code: PressMe.java


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
* An Applet that greets the user by name
*
* @author Mr J
* @version 141003
*/

public class PressMe extends Applet implements ActionListener
{
    // A text field, a button, a label
   private TextField name = new TextField("Type your name here!");
   private Button pressMe = new Button("Press me!");
   private Label greeting = new Label("The message will appear here");

/**
   * Add some objects to the Applet
   */

   public void init()
   {
     // add places objects on the Applet surface in default layout
     //(we have no control)
     add(name);
     add(pressMe);
     add(greeting);
     // Causes button presses to be detected
     pressMe.addActionListener(this);
   }
   /**
   * When an event occurs on an object with an ActionListener attached, this
   * method is carried out.
   *
   * @param carries details about the event that occurred
   */

   public void actionPerformed(ActionEvent e)
   {
    // set the label text to this String
     greeting.setText("Hello " + name.getText());
   }
}

Related: [ Java Home | previous:getting started | next:some elements of syntax ]

Teaching notes:

There is quite a lot to take in here for the first-time student. Don't try to explain everything at once - deal with the questions as they arise.

There is a lot to be gained by allowing the students to change various bits of code to see what happens.

After experimentation, maybe get them to print out the Applet window and cut & paste the relevant code sections to see the connections.


 
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 - 2007 Richard Jones, PO BOX 246, Cambridge, New Zealand; This page was last modified: July 29, 200823, 2008