Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 实战错误讨论  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教问题
liminzs





发贴: 6
积分: 0
于 2007-07-03 17:15 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我照着书上写了2个通讯程序:
发送程序:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;

public class SameAppletTesta extends Applet implements ActionListener
{
  private TextField tfObj;
  private TextField conObj;
  private String nameObj;
  private TextArea taObj;
  private String enterObj;
  public void init()
  {
    GridBagLayout gridBag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridBag);
    //jieshoufang
    Label labelObj = new Label("接受方:",Label.RIGHT);
    gridBag.setConstraints(labelObj,c);
    add(labelObj);
    tfObj = new TextField(getParameter("RECEIVERNAME"),10);
    c.fill = GridBagConstraints.HORIZONTAL;
    gridBag.setConstraints(tfObj,c);
    add(tfObj);
    tfObj.addActionListener(this);
    //fasongxiaoxi
    Label conLab = new Label("信息:",Label.RIGHT);
    gridBag.setConstraints(conLab,c);
    add(conLab);
    conObj = new TextField(16);
    gridBag.setConstraints(conObj,c);
    add(conObj);
    //anniu
    Button butObj = new Button("发送");
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.anchor = GridBagConstraints.WEST;
    c.fill = GridBagConstraints.NONE;
    gridBag.setConstraints(butObj,c);
    add(butObj);
    butObj.addActionListener(this);
    taObj = new TextArea(5,40);
    taObj.setEditable(false);
    c.anchor = GridBagConstraints.CENTER;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    gridBag.setConstraints(taObj,c);
    add(taObj);

    nameObj = getParameter("NAME");
    Label sendLabel = new Label("My Name is"+nameObj+".",Label.CENTER);
    c.weightx = 0.0;
    c.weighty = 0.0;
    gridBag.setConstraints(sendLabel,c);
    add(sendLabel);
    enterObj = System.getProperty("line.separator");
  }
  public void actionPerformed(ActionEvent event)
  {
    Applet receiver = null;
    String receiverName = tfObj.getText();
    reveiver = getAppletContext().getApplet(receiverName);
    if(receiver!=null){
      if(!(receiver instanceof SameAppletTestb)){
        taObj.append("Found applet named"+receiverName+","+"but it's not a Receiver object"+enterObj);
        }else{
        taObj.append("Found applet named"+receiverName+enterObj+"Sending massage to it."+enterObj);
        ((SameAppletTestb)receiver).processRequesFrom(nameObj,conObj.getText());
        }
    }else{
      taObj.append("Couldn't find any applet named"+receiverName+"."+enterObj);
    }
  }
}
接收程序:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SameAppletTestb extends Applet implements ActionListener{
  private final String waitingMessage = "Waiting for a massage...";
  private Label labObj = new Label(waitingMessage,Label.RIGHT);
  private TextArea taObj;
  private String lineObj;
  private String nameObj;
  public void init(){
    GridBagLayout gridBag = new GridBagLayout();
    GridBagConStraints c = new GridBagConstraints();
    setLayout(gridbag);
    Button butObj = new Button("清除");
    gridBag.setConstraints(labObj,c);
    add(LabObj);
    c.gridwidth = GridBagConstraints.REMAINDER;
    gridBag.setConstraints(butObj,c);
    add(butObj);
    butObj.addActionListener(this);
    taObj = new TextArea(5,40);
    taObj.setEditable(false);
    c.anchor = GridBagConstraints.CENTER;
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1.0;
    c.weighty = 1.0;
    gridBag.setConstraints(taObj,c);
    add(taObj);
    
    nameObj = getParameter("NAME");
    Label sendLabel = new Label("My Name is"+nameObj+".",Label.CENTER);
    c.weightx = 0.0;
    c.weighty = 0.0;
    gridBag.setConstraints(sendLabel,c);
    add(sendLabel);
  }
  public void actionPerformed(actionEvent event)
  {
    labObj.setText(waitingMessage);
    taObj.setText("");
  }
  public void processRequestfrom(String senderName,String conStr)
  {
    lineObj = System.getProperty("line.separator");
    labObj.setText("Received message from"+senderName+"!");
    taObj.append(conStr+lineObj);
  }
}
htm程序如下:
<html>
<applet code="SameAppletTesta.class" height="350" width="400" name="one">
<param name="receiverName" value="two">
</applet>
<p>
<applet code="SameAppletTestb.class" height="350" width="400">
<param name="name" value="two">
</applet>
</html>
但是程序编译不通,请高手指点哪里错了




联想游戏(欢迎跟贴,哈)

话题树型展开
人气 标题 作者 字数 发贴时间
8298 请教问题 liminzs 4181 2007-07-03 17:15
6342 Re:请教问题 ranchgirl 125 2007-07-03 18:39
7259 Re:请教问题 liminzs 85 2007-07-04 08:12

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923