Lesson Plan
 
IMOS Home Student Center TallTech Home
 

March 16, 2000

Details

Title

Applets

Time Allotment

3 hrs

Reading / References

Additional Resources

Objectives

Exercises (For Hand In)

Assignment #: JA03
Assignment: Applets
Due: Mar 21/2000
Marks: 20 marks

Demo

FOR DEMO

Greet.htm

<HTML>
<BODY>
      <APPLET CODE="Greet.class" WIDTH=300 HEIGHT=200>
</BODY>
</HTML>

Greet.java

import java.applet.*;
import java.awt.*;
 
public class Greet extends Applet
{
      Label greeting = new Label("Hello World!");
 
      public void init()
      {
            add(greeting);
      }
}

Greet.java  -  with font

import java.applet.*;
import java.awt.*;
 
public class Greet extends Applet
{
      Label greeting = new Label("Hello World!");
      Font bigFont = new Font("Arial", Font.ITALIC,24);
 
      public void init()
      {
            greeting.setFont(bigFont);
            add(greeting);
      }
}
 

Greet.java  -  with button & text box

import java.applet.*;
import java.awt.*;  

public class Greet extends Applet
{
       Label greeting = new Label("Hello World!");
       Font bigFont = new Font("Arial", Font.ITALIC,24);
 
       Button pressMe = new Button("Press Me");
       TextField answer = new TextField("",10);
 
       public void init()
       {
              greeting.setFont(bigFont);
              add(greeting);
              add(answer);
              add(pressMe);
              answer.requestFocus();
       }
}
 

Greet.java  -  with a very basic event handler

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class Greet extends Applet implements ActionListener
{
       Label greeting = new Label("Hello World!");
       Font bigFont = new Font("Arial", Font.ITALIC,24);
 
       Button pressMe = new Button("Press Me");
       TextField answer = new TextField("",10);
 
       public void init()
       {
              greeting.setFont(bigFont);
              add(greeting);
              add(answer);
              add(pressMe);
              pressMe.addActionListener(this);
 
              answer.requestFocus();
              answer.addActionListener(this);
       }
 
       public void actionPerformed(ActionEvent thisEvent)
       {
              String name = answer.getText();
 
              Label personalGreeting = new Label("");
              personalGreeting.setText("Hi " + name);
              add(personalGreeting);
 
              invalidate();
              validate();
       }
}