Java Applet:调用JavaScript – JSObject.getWindow(this)返回null

我的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代码:

       function addDrive(s) { alert(s); }   <param name="archive" value="http://localhost/LocalFileSystemApplet/bin/applet.jar?v=">        

正在加载applet,当我点击按钮时,我总是得到getDrives()返回的NULL。 为什么?

从模糊内存中,如果从init()方法调用,则建立JSObject调用将失败。

所以这..

 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); } } 

更新

我在JAVA的读/写HTML字段值中看到了它。 页面底部的“小字”表示:

为了获得最佳结果,请不要在Applet的init()方法中使用LiveConnect JSObject。

检查以下代码行:

 if (js != null) return "NULL"; 

不应该是吗?:

 if (js == null) return "NULL";