我的 Java 小程序
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JApplet;
import javax.swing.JButton;
import netscape.javascript.JSObject;
@SuppressWarnings("serial")
public class LocalFileSystem extends JApplet {
private JSObject js;
private final JButton button;
public LocalFileSystem() {
setLayout(null);
button = new JButton("getDrives()");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
button.setText( getDrives() );
}
});
button.setBounds(25, 72, 89, 23);
add(button);
}
public void init() {
js = JSObject.getWindow(this);
}
public String getDrives() {
if (js != null) return "NULL";
for (File f: File.listRoots())
js.call("addDrive", new String[] { f.getAbsolutePath() });
return "NOT NULL";
}
}
我的 HTML 代码:
<!-- language: lang-html --><html>
<head>
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function addDrive(s) {
alert(s);
}
</script>
<object type="application/x-java-applet;version=1.4.1" width="180"
height="180" name="jsap" id="applet">
<param name="archive" value="http://localhost/LocalFileSystemApplet/bin/applet.jar?v=<?php print mt_rand().time(); ?>">
<param name="code" value="LocalFileSystem.class">
<param name="mayscript" value="yes">
<param name="scriptable" value="true">
<param name="name" value="jsapplet">
</object>
</body>
</html>
小程序正在加载,当我点击按钮时,我总是得到 getDrives() 返回的 NULL。
为什么?
从模糊的记忆中,呼吁建立JSObject
如果从以下位置调用将会失败init()
method.
所以这..
public void init() {
js = JSObject.getWindow(this);
}
..应该是..
public void start() {
js = JSObject.getWindow(this);
}
自从start()
方法可能会被多次调用(例如,从最小化状态恢复浏览器),使用检查可能是值得的:
public void start() {
if (js==null) {
js = JSObject.getWindow(this);
}
}
Update
我看到它在从 JAVA 读取/写入 HTML 字段值。页面底部的“小字”指出:
为了获得最佳结果,切勿在 Applet 的 init() 方法中使用 LiveConnect JSObject。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)