Basic applet and application - Musings

By: Nicholas Duchon

Here's an example of a pretty simple applet with a few buttons and text fields with a main method that will create a JFrame to hold it, or it can be run using a web page - with comments in the code explaining how that would work. Note that main creates TWO frames and two applets, just to show how to do it.
// File: BasicApplet.java
// Author: Nicholas Duchon
// Date: March 22, 2007

// To run as a web page, see the javadoc comments

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
The following is a simple web page that will run this program, 
remove the spaces after each < to use this page:
< html> < body>
< center>
< h1>The BasicApplet by Nicholas Duchon< /h1>
< applet code=BasicApplet.java width=300 height=200>
< /applet>
< /center>
< /body> < /html>
    < ul>
      < li>
        < a href="BasicApplet.java">The source code.
      < /li>
      < li>
        < a href="runBasicApplet.html">Run the BasicApplet applet.
      < /li>
      < li>
        < a href="PrintfExample.java">PrintfExample.java source code.
      < /li>
      < li>
        < a href="ScannerExample.java">ScannerExample.java source code.
      < /li>
    < /ul>
*/

public class      BasicApplet 
       extends    JApplet 
       implements ActionListener 
       {
  
  JLabel jhb = new JLabel ("Press buttons", JLabel.CENTER);
  JLabel jht = new JLabel ("Hit return for text fields", JLabel.CENTER);
  
  JButton jb1 = new JButton ("one");
  JButton jb2 = new JButton ("two");
  JButton jb3 = new JButton ("three");
  JButton jb4 = new JButton ("four");
  JButton jb5 = new JButton ("five");
  JButton jb6 = new JButton ("six");
  JTextField jt1 = new JTextField ("one");
  JTextField jt2 = new JTextField ("one");
  JTextField jt3 = new JTextField ("one");
  JTextField jt4 = new JTextField ("one");
  JTextField jt5 = new JTextField ("one");
  JTextField jt6 = new JTextField ("one");
  JLabel jL1 = new JLabel ("Input echo:", JLabel.RIGHT);
  JTextField jtz = new JTextField ();
  
  
  public void init () {
    Container c = getContentPane ();
    c.setLayout (new GridLayout (0, 2));
    
    c.add (jhb);
    c.add (jht);
    
    c.add (jb1);
    c.add (jt1);
    c.add (jb2);
    c.add (jt2);
    c.add (jb3);
    c.add (jt3);
    c.add (jb4);
    c.add (jt4);
    c.add (jb5);
    c.add (jt5);
    c.add (jb6);
    c.add (jt6);
    c.add (jL1);
    c.add (jtz);
    
    jb1.addActionListener (this);
    jb2.addActionListener (this);
    jb3.addActionListener (this);
    jb4.addActionListener (this);
    jb5.addActionListener (this);
    jb6.addActionListener (this);
    
    jt1.addActionListener (this);
    jt2.addActionListener (this);
    jt3.addActionListener (this);
    jt4.addActionListener (this);
    jt5.addActionListener (this);
    jt6.addActionListener (this);
  } // end method init
  
  public void actionPerformed (ActionEvent e) {
    Object s = e.getSource ();
    if (s == jb1) jtz.setText ("Button one");
    if (s == jb2) jtz.setText ("Button two");
    if (s == jb3) jtz.setText ("Button three");
    if (s == jb4) jtz.setText ("Button four");
    if (s == jb5) jtz.setText ("Button five");
    if (s == jb6) jtz.setText ("Button six");
    
    if (s == jt1) jtz.setText (jt1.getText());
    if (s == jt2) jtz.setText (jt2.getText());
    if (s == jt3) jtz.setText (jt3.getText());
    if (s == jt4) jtz.setText (jt4.getText());
    if (s == jt5) jtz.setText (jt5.getText());
    if (s == jt6) jtz.setText (jt6.getText());
  } // end method ActionPerformed
  
  public static void main (String args []) {
         String title = "BasicApplet by Nick Duchon";
         JFrame hostFrame = new JFrame ();  // create a display window
         hostFrame.setTitle (title);        // sets display window's title
         hostFrame.setSize (300,200);       // set the display window size, MUST set this value
         BasicApplet a = new BasicApplet ();          // instantiate the applet
         hostFrame.add(a);                  // add applet to window frame
         a.init();                          // init applet
         a.start();                         // start applet
      
         String title2 = "BasicApplet - Number 2"; // title of second window
         JFrame hostFrame2 = new JFrame ();  // create a second display window
         hostFrame2.setTitle (title2);        // sets display window's title
         hostFrame2.setSize (300,200);       // set the display window size, MUST set this value
         BasicApplet a2 = new BasicApplet ();          // instantiate the applet a second time
         hostFrame2.add(a2);                  // add the second applet to the second window frame
         a2.init();                          // init applet number 2
         a2.start();                         // start applet number 2
         hostFrame2.setVisible (true);       // show the second applet
      
         hostFrame.setVisible (true);       // show first applet, default: window hidden
                                            // next line exits on x button of window
         hostFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

// The OLD style, EXIT_ON_CLOSE since JDK 1.3
//          hostFrame.addWindowListener(       // make sure application quits on x button
//                 new WindowAdapter() {
//                    public void windowClosing (WindowEvent e) {
//                      System.exit (0);
//                   } // end window closing event handler
//                } // end window adapter
//             ); // end window listenter

  } // end call with main, also.
  
} // end class BasicApplet

ND.