Tutorial: Building an applet

Overview Step 6 Step 7 Step 10

Applet source code

Applet HTML source code

Source code for GoodEveningApplet.html:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<TITLE>
Good Evening HTML Page
</TITLE>
</HEAD>
<BODY>
firstapplet.GoodEveningApplet will appear below in a Java enabled browser.
<APPLET CODEBASE = "." CODE = "firstapplet.GoodEveningApplet.class" ARCHIVE = "GoodEvening.jar" NAME = "TestApplet" WIDTH = 400 HEIGHT = 300 HSPACE = 0 VSPACE = 0 ALIGN = middle >

You need a Java-enabled browser to view this applet.

</APPLET> </BODY> </HTML>

Applet class source code

Source code for GoodEveningApplet.java:
package firstapplet;

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

public class GoodEveningApplet extends Applet {
  boolean isStandalone = false;
  BorderLayout borderLayout1 = new BorderLayout();
  Panel panel1 = new Panel();
  BorderLayout borderLayout2 = new BorderLayout();
  Panel lower = new Panel();
  Panel upper = new Panel();
  CardLayout cardLayout1 = new CardLayout();  
  BorderLayout borderLayout4 = new BorderLayout();
  Panel panel2 = new Panel();
  Panel panel3 = new Panel();
  Panel panel4 = new Panel();
  Panel panel5 = new Panel();
  Panel panel6 = new Panel();
  Choice choice1 = new Choice();
  Label label1 = new Label();
  Label label2 = new Label();
  Label label3 = new Label();
  Label label4 = new Label();
  Label label5 = new Label();
  Label label6 = new Label();
  BorderLayout borderLayout3 = new BorderLayout();
  BorderLayout borderLayout5 = new BorderLayout();
  BorderLayout borderLayout6 = new BorderLayout();
  BorderLayout borderLayout7 = new BorderLayout();
  Button button1 = new Button();
  
  /**Get a parameter value*/  
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public GoodEveningApplet() {
  }

  /**Initialize the applet*/
  public void init() {
    choice1.addItem("English");
    choice1.addItem("German");
    choice1.addItem("Pig Latin");
    choice1.addItem("Swedish");
    choice1.addItem("Australian");
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  /**Component initialization*/
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    panel1.setLayout(borderLayout2);
    upper.setBackground(Color.orange);
    lower.setBackground(Color.magenta);
    lower.setLayout(cardLayout1);
    panel2.setBackground(new Color(190, 173, 255));
    panel2.setLayout(borderLayout3);
    panel3.setBackground(new Color(83, 182, 255));
    panel3.setLayout(borderLayout5);
    panel4.setBackground(new Color(255, 149, 66));
    panel4.setLayout(borderLayout6);
    panel5.setBackground(new Color(239, 107, 140));
    panel5.setLayout(borderLayout7);
    panel6.setBackground(new Color(17, 198, 99));
    panel6.setLayout(null);
    choice1.addItemListener(new java.awt.event.ItemListener() {

      public void itemStateChanged(ItemEvent e) {
        choice1_itemStateChanged(e);
      }
    });
    label1.setFont(new java.awt.Font("Serif", 1, 20));
    label1.setForeground(Color.blue);
    label1.setText("Select a language");
    label2.setFont(new java.awt.Font("Dialog", 1, 24));
    label2.setForeground(Color.black);
    label2.setText("Good Evening");
    label3.setFont(new java.awt.Font("Dialog", 1, 24));
    label3.setForeground(Color.black);
    label3.setText("Guten Abend");
    label4.setFont(new java.awt.Font("Dialog", 1, 24));
    label4.setForeground(Color.black);
    label4.setText("Oodgay vening eay");
    label5.setFont(new java.awt.Font("Dialog", 1, 24));
    label5.setForeground(Color.black);
    label5.setText("God Kväll");
    label6.setFont(new java.awt.Font("Dialog", 1, 24));
    label6.setForeground(Color.black);
    label6.setText("Gudday, Mate");
    label6.setBounds(new Rectangle(134, 121, 183, 58));
    button1.setLabel("Push Me");
    button1.setBounds(new Rectangle(160, 60, 107, 35));
    button1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        button1_actionPerformed(e);
      }
    });
    this.add(panel1, BorderLayout.CENTER);
    panel1.add(lower, BorderLayout.CENTER);
    lower.add(panel2, "panel2");
    panel2.add(label2, BorderLayout.NORTH);
    lower.add(panel3, "panel3");
    panel3.add(label3, BorderLayout.SOUTH);
    lower.add(panel4, "panel4");
    panel4.add(label4, BorderLayout.EAST);
    lower.add(panel5, "panel5");
    panel5.add(label5, BorderLayout.WEST);
    lower.add(panel6, "panel6");
    panel6.add(button1, null);
    panel6.add(label6, null);
    panel1.add(upper, BorderLayout.NORTH);
    upper.add(label1, null);
    upper.add(choice1, null);
  }

  /**Start the applet*/
  public void start() {
  }

  /**Stop the applet*/
  public void stop() {
  }

  /**Destroy the applet*/
  public void destroy() {
  }

  /**Get Applet information*/
  public String getAppletInfo() {
    return "Applet Information";
  }

  /**Get parameter info*/
  public String[][] getParameterInfo() {
    return null;
  }

  void choice1_itemStateChanged(ItemEvent e) {
    if (choice1.getSelectedItem()== "English") {
      cardLayout1.show(lower, "panel2");
    }
    else if (choice1.getSelectedItem()== "German") {
      cardLayout1.show(lower, "panel3");
    }
    else if (choice1.getSelectedItem()== "Pig Latin") {
      cardLayout1.show(lower, "panel4");
    }
    else if (choice1.getSelectedItem()== "Swedish") {
      cardLayout1.show(lower, "panel5");
    }
    else if (choice1.getSelectedItem()== "Australian") {
      cardLayout1.show(lower, "panel6");
    }
  }

  void button1_actionPerformed(ActionEvent e) {
    label6.setForeground(new Color(255,0,0));
  }


}

Overview Step 6 Step 10