我的GWT Java类中有一个原生Javascript方法,但是我在从本地Javascript代码调用Java方法时遇到了困难。我试图尽可能密切地跟踪这,但我无法让它开始工作。我编译了它并在Firefox中运行它,错误控制台说“错误: this.lc不是一个函数”。我试着把所有的方法都改为公开的,但这似乎没有什么区别。我做错了什么?
package com.proprintsgear.design_lab.client;
...
public class ValueBox extends HorizontalPanel {
...
private void fireChange() {
...
}
private void increaseValue() {
...
}
private native void addNativeMouseWheelListener(String id) /*-{
function mouseOverHandler(e) {
$wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
}
function mouseOutHandler(e) {
$wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
}
function scrollWheelMove(e) {
if ($wnd.event || $wnd.Event) {
if (!e) e = $wnd.event;
if (e.wheelDelta <= 0 || e.detail > 0 ) {
$wnd.alert("DOWN");
} else {
this.@com.proprintsgear.design_lab.client.ValueBox::increaseValue()();
}
this.@com.proprintsgear.design_lab.client.ValueBox::fireChange()();
}
}
var box=$doc.getElementById(id);
box.addEventListener("mouseout",mouseOutHandler,false);
box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;发布于 2009-06-12 01:04:08
在我过去做过的所有代码中,我从来没有使用“this”来标识我的类,我已经通过了这个类。
改变这一点:
private native void addNativeMouseWheelListener(String id) /*-{
function mouseOverHandler(e) {
$wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
}
function mouseOutHandler(e) {
$wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
}
function scrollWheelMove(e) {
if ($wnd.event || $wnd.Event) {
if (!e) e = $wnd.event;
if (e.wheelDelta <= 0 || e.detail > 0 ) {
$wnd.alert("DOWN");
} else {
this.@com.proprintsgear.design_lab.client.ValueBox::increaseValue()();
}
this.@com.proprintsgear.design_lab.client.ValueBox::fireChange()();
}
}
var box=$doc.getElementById(id);
box.addEventListener("mouseout",mouseOutHandler,false);
box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;对此:
private native void addNativeMouseWheelListener(ValueBox instance, String id) /*-{
function mouseOverHandler(e) {
$wnd.addEventListener("DOMMouseScroll", scrollWheelMove, false);
}
function mouseOutHandler(e) {
$wnd.removeEventListener("DOMMouseScroll", scrollWheelMove, false);
}
function scrollWheelMove(e) {
if ($wnd.event || $wnd.Event) {
if (!e) e = $wnd.event;
if (e.wheelDelta <= 0 || e.detail > 0 ) {
$wnd.alert("DOWN");
} else {
instance.@com.proprintsgear.design_lab.client.ValueBox::increaseValue()();
}
instance.@com.proprintsgear.design_lab.client.ValueBox::fireChange()();
}
}
var box=$doc.getElementById(id);
box.addEventListener("mouseout",mouseOutHandler,false);
box.addEventListener("mouseover",mouseOverHandler,false);
}-*/;发布于 2013-02-05 00:17:16
我找到了更好的方法。这与您在JavaScript中所做的类似,您在其中设置了"var that = this“。使用这种方法,您不必将其传递给listenForPostMessage():
protected native void postMessage(String msg) /*-{
$wnd.postMessage(msg, "*");
}-*/;
private final native void listenForPostMessage() /*-{
var that = this;
$wnd.addEventListener("message", function(msg) {
that.@org.dartlang.gwtapplication.client.GwtApplication::onPostMessage(Ljava/lang/String;Ljava/lang/String;)(
msg.data, msg.origin);
});
}-*/;
private void onPostMessage(String data, String origin) {
Label msgLabel = new Label();
msgLabel.setText("GWT received a postMessage: Data: " +
data + " Origin: " + origin);
mainPanel.add(msgLabel);
}https://stackoverflow.com/questions/981924
复制相似问题