Java小程序:调用JavaScript-JSObject.getWindow(this)总是返回null
Java Applet: Call JavaScript - JSObject.getWindow(this) returns always null
我的Java Applet
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。为什么?
从模糊内存中,如果从init()
方法调用,则建立JSObject
的调用将失败。
所以这个。。
public void init() {
js = JSObject.getWindow(this);
}
应该是。。
public void start() {
js = JSObject.getWindow(this);
}
由于start()
方法可能被调用多次(例如,从最小化状态恢复浏览器),因此使用check:可能是值得的
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";
相关文章:
- 数组在递归方法中设置为null
- $(this).prop('property') vs. this.property
- Twitter Bootstrap typeahead:使用“this”获取上下文/调用元素
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 在另一个函数中使用变量this
- 来自文本输入null的html javascript变量
- ui.selectmenu投掷'this.newelement.0'为null或不是对象
- Ext.getCmp('panel')给出this.el为null或不是对象
- TypeError: this.element is null, Rails with Judge gem
- 揭示模块模式:func.apply(null,arr) vs. func.apply(this,arr),其中 func
- Javascript未定义错误:“this”为null
- IE 8中的jQuery错误:消息:'this.0.ownerDocument.body'为null或不
- HTML Selection - Javascript "this.value" null
- Java小程序:调用JavaScript-JSObject.getWindow(this)总是返回null
- "类型错误:this.options为null"在OpenLayers.Protocol.HTTP中
- Reactjs this.state 给出 Uncatch TypeError: 无法读取 null 的属性 'grou
- React Store为' this '返回null值
- 为什么'this.props.children;null用于react路由器(react Redux)