从JavaScript调用Java小程序的功能 [英] Call Java Applet function from Javascript
问题描述
我试图做一个基本的Java小程序来打开客户端对他们的计算机上的文件。我想通过下面的JavaScript来调用Java小程序的功能中openFile
进口的java.awt.Desktop;
进口的java.io.File;
进口java.io.IOException异常;进口javax.swing.JApplet中;公共类测试扩展JApplet的{
公共无效中openFile(字符串文件路径){
文件f =新的文件(文件路径); 尝试{
Desktop.getDesktop()开(F)。
}赶上(IOException异常五){
e.printStackTrace();
}
}
}
在我的网页的body标签我有以下的:
<小程序code =的Test.classHEIGHT =0WIDTH =0> < /小程序><脚本类型=文/ JavaScript的>
document.applets [0] .openFile(C:\\\\ test.log中);
< / SCRIPT>
当我加载页面出现错误:
类型错误:对象#没有方法中openFile
块引用>有谁知道我需要做什么来解决这个错误,并得到该applet的工作?
解决方案<脚本的src =
http://www.java.com/js/deployJava.js\"></script>
&LT;脚本&GT;
&所述;! - 小程序ID可用于得到一个参考
applet的对象 - &GT;
VAR属性= {ID:'mathApplet',
code:'jstojava.MathApplet',宽度:1,身高:1};
VAR参数= {jnlp_href:数学applet.jnlp'};
deployJava.runApplet(属性,参数,'1.6');
&LT; / SCRIPT&GT;参考:<一href=\"http://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html\"相对=nofollow>小程序中调用方法从JavaScript的
更新
JavaScript是不允许直接调用applet的公共方法或公共变量。的JavaScript认为嵌入式小程序作为对象。通过提供Applet的一个ID,使用Javascript可以访问它。
document.Applet_ID.Applet_Method()
和您可以使用此,
MyApplet.html启动
&LT; HTML和GT;
&LT; HEAD&GT;
&LT; SCRIPT LANGUAGE =使用Javascript&GT;
功能accessAppletMethod()
{
的document.getElementById(AppletABC)AppendText通过(小程序的方法);
}
&LT; / SCRIPT&GT;&LT;标题&GT;测试与LT; /标题&GT;&LT; /头&GT;
&LT;身体的onload =accessAppletMethod()&GT;&LT; H1&GT;的Javascript接取Applet的方法和LT; / H1&GT;
&LT;小程序WIDTH = 300 HEIGHT = 100 n =AppletABC
code =JavaScriptToJava.class&GT;
&LT; /小程序&GT;&LT; /身体GT;
&LT; / HTML&GT;JavaScriptToJava.java
进口java.applet.Applet中;
进口java.awt.FlowLayout中;
进口java.awt.TextArea中;公共类JavaScriptToJava扩展的Applet { 文本区域的textBox; 公共无效的init(){
的setLayout(新的FlowLayout());
textBox中=新文本区(5,40);
添加(的textBox);
} 公共无效AppendText通过(字符串文本){
textBox.append(文本);
}
}I am trying to make a basic Java Applet to open a file on the client's computer for them. I would like to call the openFile function in the java applet below via javascript.
import java.awt.Desktop; import java.io.File; import java.io.IOException; import javax.swing.JApplet; public class Test extends JApplet { public void openFile(String filePath) { File f = new File(filePath); try { Desktop.getDesktop().open(f); } catch (IOException e) { e.printStackTrace(); } } }
In between the body tags of my webpage I have the following:
<applet code="Test.class" height="0" width="0"></applet> <script type="text/javascript"> document.applets[0].openFile("C:\\test.log"); </script>
When I load the page I get the error:
TypeError: Object # has no method 'openFile'
Does anyone know what I need to do to fix this error and get the applet working?
解决方案<script src= "http://www.java.com/js/deployJava.js"></script> <script> <!-- applet id can be used to get a reference to the applet object --> var attributes = { id:'mathApplet', code:'jstojava.MathApplet', width:1, height:1} ; var parameters = {jnlp_href: 'math-applet.jnlp'} ; deployJava.runApplet(attributes, parameters, '1.6'); </script>
Reference : Invoking Applet Methods From JavaScript
Update
Javascript is allowed to directly call Applet’s public methods or public variables. Javascript considers the embedded Applet as an object. By providing the Applet with an ID, Javascript can access it with
document.Applet_ID.Applet_Method()
and you can use this,
MyApplet.html
<html> <head> <script language="Javascript"> function accessAppletMethod() { document.getElementById("AppletABC").appendText("Applet Method"); } </script> <title>Testing</title></head> <body onload="accessAppletMethod()"> <h1>Javascript acess Applet method</h1> <applet width=300 height=100 id="AppletABC" code="JavaScriptToJava.class"> </applet> </body> </html>
JavaScriptToJava.java
import java.applet.Applet; import java.awt.FlowLayout; import java.awt.TextArea; public class JavaScriptToJava extends Applet{ TextArea textBox; public void init(){ setLayout(new FlowLayout()); textBox = new TextArea(5,40); add(textBox); } public void appendText(String text){ textBox.append(text); } }
这篇关于从JavaScript调用Java小程序的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!