我今天在以下位置看到了一篇关于 Javascript_Java_Interaction 的文章:
http://www.rgagnon.com/javadetails/java-0184.html
[ You can try the working version on that site ]
So I tried it on my PC, after some simple format change the files look like this :
==================================================== ===========================================
[C:/Dir_Fit/Javascript_Java_Interaction.html]
<FRAMESET ROWS="100%,*">
<FRAME NAME="mainFrame" SRC="Javascript_Java_Interaction_Visible_Page.html" border=0>
<FRAME NAME="scriptFrame" SRC="Javascript_Java_Interaction_Invisible_Page.html" border=0>
</FRAMESET>
--------------------------------------------------------------------------------------------
[C:/Dir_Fit/Javascript_Java_Interaction_Invisible_Page.html]
<HTML>
<HEAD>
<SCRIPT>
function replace(s,t,u)
{
i=s.indexOf(t);
r="";
if (i==-1) return s;
r+=s.substring(0,i)+u;
if (i+t.length<s.length) r+=replace(s.substring(i+t.length,s.length),t,u);
return r;
}
function getAndSendMessage()
{
theMessage=document.location.search.substring(1,255)
if (theMessage.length>0)
{
// replace all '+" by space
theMessage=replace(theMessage,'+',' ')
window.parent.mainFrame.showMessage(unescape(theMessage))
}
}
</SCRIPT>
</HEAD>
<BODY onLoad="getAndSendMessage();"></BODY>
</HTML>
-----------------------------------------------------------------------------------------
[C:/Dir_Fit/Javascript_Java_Interaction_Visible_Page.html]
<HTML>
<HEAD><SCRIPT>function showMessage(s) { alert(s) }</SCRIPT></HEAD>
<BODY>
<H1>Simple Java - Javascript interaction</H1><P>
<APPLET CODEBASE="file://C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
</BODY>
</HTML>
-----------------------------------------------------------------------------------------
Source : [C:/Dir_Fit/src/Javascript_Java_Interaction_Demo.java]
class : [C:/Dir_Fit/build/classes/Javascript_Java_Interaction_Demo.class]
import java.applet.Applet;
import java.awt.*;
public class Javascript_Java_Interaction_Demo extends Applet
{
TextField aMessage;
Button sendButton;
public void init()
{
aMessage=new TextField(20);
add(aMessage);
sendButton=new Button("Send to Javascript");
add(sendButton);
}
public boolean action(Event e,Object o)
{
if (e.target.equals(sendButton))
{
try { getAppletContext().showDocument(new java.net.URL(getCodeBase(),"Javascript_Java_Interaction_Invisible_Page.html?"+java.net.URLEncoder.encode(aMessage.getText())),"scriptFrame"); }
catch (Exception ex) { ex.printStackTrace(); }
}
return true;
}
}
==================================================== =========================================
它在页面 [Javascript_Java_Interaction.html] 上显示了小程序,但是如果我输入一些文本并单击按钮,则没有任何反应,问题出在以下行:
<APPLET CODEBASE="file://C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
所以我将类文件复制到与 html 文件 [C:/Dir_Fit/] 相同的目录中,并将上面的行更改为:
<APPLET CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
现在它可以工作了,它将显示一个警报窗口。
所以,我的问题是为什么当我指定代码库时它不起作用?根据Java文档,如果类文件位于不同的目录中,您可以通过代码库告诉它,小程序将显示,但如果您输入一些文本并单击按钮,则不会发生任何事情,我尝试了不同的方法来指定代码库:
<APPLET CODEBASE="file:///C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
<APPLET CODEBASE="C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
<APPLET CODEBASE="build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
<APPLET CODE="C:/Dir_Fit/build/classes/Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150></APPLET>
然而,它们都不起作用,为什么?我不希望类文件与 html 位于同一目录中。有什么解决办法?
坦率