一个关于applet的问题。。。 applet程序本来就不能对文件操作啊用网页表但处理吧 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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> 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.javaimport 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.javaimport 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); } } 请大家帮忙改错 小问题??? 关与javamail的问题,菜鸟提问 关于解析网络xml的中文问题 如何利用jdbc一次操作多个数据? jsp与acess的连接,书上的例子,i am a newer 请各位帮忙做个题目 请教这段代码是什么意思啊? 《编程高手箴言 》大家看过没有,讨论一下! 2分的小问题。。。 请帮帮我这个门外汉!(在线等待!!) 学 JAVA 要与 UNIX/Linux 结合起来学习吗?
当获取了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>
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.javaimport 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.javaimport 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); } }