Have Applets on the same page communicate with each other
[Applet1] import java.awt.*;
public class Applet1 extends java.applet.Applet {
  TextField inputText;
  Button    b;  public void init() {
    setLayout(new FlowLayout());
    inputText = new TextField( "", 15 );
    b = new Button("Send to Applet 2");
    add(inputText);
    add(b);    
    }  public boolean action(Event ev, Object arg) { 
    if (ev.target instanceof Button) { 
       String textMsg = inputText.getText().trim();
       Applet2 applet2 = 
         (Applet2)getAppletContext().getApplet("applet2");
       if ( applet2 != null ) {
          applet2.AppendText( textMsg );
          return true;
          }
       else
          return false;
       }
     return false;
     } [Applet2] import java.awt.*;public class Applet2 extends java.applet.Applet {
  TextArea textBox;  public void init() {
    setLayout(new FlowLayout());
    textBox = new TextArea( 5, 40 );
    add( textBox );
    }  public void AppendText( String msg ) {
    String newLine = new String( "\n" );
    textBox.appendText( msg );
    textBox.appendText( newLine );
    }
  } [HTML] <HTML><HEAD></HEAD><BODY>
<APPLET CODE="Applet1.class"  
        NAME="applet1" 
        HEIGHT=200 WIDTH=150>
</APPLET>
<APPLET CODE="Applet2.class"  
        NAME="applet2" 
        HEIGHT=200 WIDTH=400>
</APPLET>
</BODY></HEAD>
 

解决方案 »

  1.   

    Have Applets on different frames communicates with each other
    It is possible to share data between different applets via static variables[HTML (main.html)] <HTML><HEAD></HEAD>
    <FRAMESET COLS="50%,*">
        <FRAME SRC="f1.html" NAME="f1">
        <FRAME SRC="f2.html" NAME="f2">
    </FRAMESET>
    </HEAD> [HTML (f1.html AND f2.html)]      
    lt;HTML>lt;HEAD>lt;/HEAD>
    lt;BODY>
    lt;APPLET CODE="app.class" 
            NAME="app1" 
            HEIGHT=200 
            WIDTH=200>
    lt;/APPLET>
    lt;/BODY>lt;/HTML> [JAVA source (app.java)] import java.awt.*;
    import java.applet.*;public class app extends Applet {
      TextField tf;
      Button    a,b;  public void init() {
        setLayout(new FlowLayout()); 
        a  = new Button("Send");
        b  = new Button("Receive");
        add(a);
        add(b);
        tf = new TextField(20);
        add(tf);
       }  public boolean action(Event e, Object o) {
        if (e.target instanceof Button) {  
           if (e.target == a) {
              StaticMessage.message = tf.getText();
              }
           if (e.target == b) {
              tf.setText(StaticMessage.message);
              }
           return true;
           }
        return false;
        }
      }class StaticMessage {
      public static String message = "";
      } 
      

  2.   

    Have an applet launch an other applet
    The idea is to load first a small Applet with a quick loading time, display a message to the user and then load a larger Applet.[HTML (testQuick.html) <HTML><HEAD></HEAD><BODY>
    <APPLET CODE="QLoader.class" 
            NAME="QLoader"
            HEIGHT=200 
            WIDTH=200>
    <PARAM NAME="appletToLoad" VALUE="SecondApplet">
    <PARAM NAME="SecondAppletParm" VALUE="Hello World">
    </APPLET></BODY></HTML> [JAVA source (QLoader.java)] import java.applet.Applet;
    import java.applet.AppletStub;
    import java.awt.*;public class QLoader extends Applet 
        implements Runnable, AppletStub {
      String appletToLoad;
      Thread appletThread;  public void init() {
        appletToLoad = getParameter("appletToLoad");
        setBackground(Color.white);
        }  public void paint(Graphics g) {
        g.drawString("Loading the BIG ONE ...", 30, 30);
        }   public void run() {
        try {
          Class appletClass = Class.forName(appletToLoad);
          Applet realApplet = (Applet)appletClass.newInstance();
          realApplet.setStub(this);
          setLayout( new GridLayout(1,0));
          add(realApplet);
          realApplet.init();
          realApplet.start();
          }
        catch (Exception e) {
          System.out.println( e );
          }
        validate();
        }  public void start(){
        appletThread = new Thread(this);
        appletThread.start();
        }  public void stop() {
        appletThread.stop();
        appletThread = null;
        }  public void appletResize( int width, int height ){
        resize( width, height );
        }
    } [SecondApplet.java for demonstration] import java.awt.*;  public class SecondApplet extends java.applet.Applet {
        TextField tf;
        public void init() {
          System.out.println("Starting Second applet");
          String s;
          tf = new TextField( 10 );
          add( tf );
          s = getParameter("SecondAppletParm");
          tf.setText(s);
          }
        }