一个关于applet的问题。。。

gzyip 2004-04-15 03:51:06
我有两个applet,分别叫做applet1和applet2,它们分别在网页web1.htm和web2.htm里面.
打开web1.htm就会运行applet1,applet1在处理键盘事件后会打开web2.htm,applet2就运行在web2.htm里面.
我现在想把applet1里面的一个变量传到applet2中去,请问有什么办法???

附:我试过写入文件,但是抛出异常,说没有权限处理文件.
...全文
91 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhengoodman 2004-04-16
  • 打赏
  • 举报
回复
Sender.java
nameField =new TextField( getParameter (
"RECEIVERNAME"),10 );
...
Applet receiver = null;
String receiverName = nameField.getText(); //Get name to search for.
receiver = getAppletContext().getApplet(receiverName);
if (receiver != null) {
if ( !(receiver instanceof Receiver)){
status.appendText("Found applet named"
+ receiverName + ", "
+"but it's not a Receiver object.\n");
}else{
status.appendText("Found applet named"
+ receiverName + ".\n"
+ " Sending message to it.\n");
//Cast the receiver to be a Receiver object
// so that the compiler will let us call a Receiver method.
((Receiver)receiver).processRequestFrom(myName);
}
}


Receiver.java
public void processRequestFrom(String sName){
label.setText("Received message from "+ sName + "!");
}



(4)程序:Sender.java

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Enumeration;

public class Sender extends Applet implements ActionListener {

private String myName;

private TextField nameField;

private TextArea status;

private String newline;

public void init() {

GridBagLayout gridBag = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();

setLayout(gridBag);

Label receiverLabel = new Label("Receiver name:",Label.RIGHT);

gridBag.setConstraints(receiverLabel, c);

add(receiverLabel);

//获得接收者名

nameField = new TextField(getParameter("RECEIVERNAME"),10);

c.fill = GridBagConstraints.HORIZONTAL;

gridBag.setConstraints(nameField, c);

add(nameField);

nameField.addActionListener(this);

Button button = new Button("Send message");

c.gridwidth = GridBagConstraints.REMAINDER;

c.anchor = GridBagConstraints.WEST;

c.fill = GridBagConstraints.NONE;

gridBag.setConstraints(button, c);

add(button);

button.addActionListener(this);

status = new TextArea(5, 60);

status.setEditable(false);

c.anchor = GridBagConstraints.CENTER;

c.fill = GridBagConstraints.BOTH;

c.weightx = 1.0;

c.weighty = 1.0;

gridBag.setConstraints(status, c);

add(status);

myName = getParameter("NAME"); //获得本applet的名字

Label senderLabel = new Label("(My name is " + myName + ".)",Label.CENTER);

c.weightx = 0.0;

c.weighty = 0.0;

gridBag.setConstraints(senderLabel, c);

add(senderLabel);

newline = System.getProperty("line.separator"); //获得系统的换行符

}

public void actionPerformed(ActionEvent event) {

Applet receiver = null; //声明一个applet对象

String receiverName = nameField.getText(); //获得接收者名

receiver = getAppletContext().getApplet(receiverName); //创建接收者

if (receiver != null) {

if (!(receiver instanceof Receiver)) { //判断是否为接收者

status.append("Found applet named "+ receiverName + ", "

+ "but it's not a Receiver object." + newline);

}else{

status.append("Found applet named "+ receiverName + newline

+ " Sending message to it." + newline);

//把对象receiver转换成Receiver类型,这样就可以调用Receiver的方法。

(Receiver)receiver).processRequestFrom(myName); //设置收到的信息

}

} else {

status.append("Couldn't find any applet named "

+ receiverName + "." + newline);

}

}

public Insets getInsets(){ //覆盖getInsets方法,设置容器的周边环境

return new Insets(3,3,3,3);

}

public void paint(Graphics g) {

g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

}

public String getAppletInfo() {

return "Sender by Kathy Walrath";

}

}

Receiver.java

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class Receiver extends Applet implements ActionListener {

private final String waitingMessage = "Waiting for a message... ";

private Label label = new Label(waitingMessage, Label.RIGHT);

public void init() {

Button button = new Button("Clear");

add(label);

add(button);

button.addActionListener(this);

add(new Label("(My name is " + getParameter("name") + ".)", Label.LEFT));

}

public void actionPerformed(ActionEvent event) {

label.setText(waitingMessage); //清除收到的信息

}

public void processRequestFrom(String senderName) { //设置受到的信息

label.setText("Received message from " + senderName + "!");

}

public void paint(Graphics g) {

g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);

}

}

zhengoodman 2004-04-16
  • 打赏
  • 举报
回复
Applets 之间的互访

Applets 可以与其它applets进行通信互访。

一个applet 可以通过名字(用AppletContext的getApplet方法)或通过获取页面中所有的applets (用AppletContext的 getApplets方法)来获取applets。
当获取了applet对象后,即可调用该对象的方法。
例1,通过名字获取Applet

getApplet 方法搜索当前页中的所有applets,若有与其指定的名相同的applet则返回一个该applet的Applet对象,然后调用该对象的方法。在缺省状态下,applet是没有名字的,必须通过<applet>标记来给它命名。下面是两个applets通信的实例。

(1)用选项NAME给applets命名,如:

<APPLET CODEBASE=example/ CODE=Sender.class

WIDTH=450 HEIGHT=200 NAME="buddy">

<param NAME =" RECEIVERNAME " value="old pal" >

. . .

</applet>

(2)用<PARAM> 标记命名,如:

<APPLET CODEBASE=example/ CODE=Receiver.class

WIDTH=450 HEIGHT=35>

<PARAM NAME="name" value="old pal" >

. . .

</applet>

liefeng123 2004-04-16
  • 打赏
  • 举报
回复
applet程序本来就不能对文件操作啊

用网页表但处理吧
gzyip 2004-04-16
  • 打赏
  • 举报
回复
自己顶。。。
gzyip 2004-04-15
  • 打赏
  • 举报
回复
急啊。。。帮一下忙啦。。。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧