日期:2014-05-20  浏览次数:20638 次

Applet之间单向通信
Java code

// AppletTalkA.java

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class AppletTalkA extends Applet {
    Button sendB;
    TextField mesgField;
    public void init() {
        mesgField=new TextField(30);
        sendB=new Button("Send");
        sendB.addActionListener(this);//编译不通过
    }
 
  
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==sendB){
            String mesg=mesgField.getText();
            AppletTalkB receiver=(AppletTalkB)getAppletContext().getApplet("receiver");
            if(receiver!=null) receiver.getMessage(mesg);
            mesgField.setText("");
            }            
    }

}


// AppletTalkB.java

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

public class AppletTalkB extends Applet{
    TextArea mesgArea;
    public void init(){
        mesgArea=new TextArea(10,30);
        
        }
    public void getMessage(String mesg){
        mesgArea.append(mesg+"\n");
        }
    
    
    }






实现Applet之间单向通信,第一次做这种题目,网上和书本介绍都很少,自己调不过来,希望大家帮忙一下,谢谢!



------解决方案--------------------
加了监听之后要扩展功能,public class AppletTalkA extends Applet应该改成public class AppletTalkA extends Applet implements ActionListener