GWT JSNI 布尔值

发布于 2024-11-14 03:31:25 字数 1807 浏览 6 评论 0原文

这是我的代码:

package com.eggproject_hu.WPECommerceAdminSales.client;

import java.lang.Boolean;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

public class AblakVillogo
{
    public static Boolean focusedWindow = true;
    private static Boolean init = false;

    public static void setFocused(Boolean focus)
    {
        focusedWindow = focus;
    }

    public static Boolean getFocused()
    {
        return focusedWindow;
    }

    public static void focusVizsgalat()
    {
        if(focusedWindow == true)
        {
            GWT.log("igen");
        }
        else
        {
            GWT.log("nem");
        }
    }

    public static void init()
    {
        if(init == false)
        {
            _init();
        }
    }

    private native static void _init() /*-{
        $wnd.jQuery(document).ready(function()
        {
            $wnd.jQuery($wnd).focus(function()
            {
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();               @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(true);
                $wnd.console.log("focus");
            }).blur(function()
            {
                var ret = false;
                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();
                                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(false);
                $wnd.console.log("blur");
            });
        });
    }-*/;
}

我在浏览器控制台中看到了这一点:

未捕获的异常:java.lang.IllegalArgumentException:调用参数:布尔类型的 JS 值,预期为 java.lang.Boolean

我已经在Chrome和Firefox中进行了测试。

问题是什么?

感谢您的帮助!

Here's my code:

package com.eggproject_hu.WPECommerceAdminSales.client;

import java.lang.Boolean;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;

public class AblakVillogo
{
    public static Boolean focusedWindow = true;
    private static Boolean init = false;

    public static void setFocused(Boolean focus)
    {
        focusedWindow = focus;
    }

    public static Boolean getFocused()
    {
        return focusedWindow;
    }

    public static void focusVizsgalat()
    {
        if(focusedWindow == true)
        {
            GWT.log("igen");
        }
        else
        {
            GWT.log("nem");
        }
    }

    public static void init()
    {
        if(init == false)
        {
            _init();
        }
    }

    private native static void _init() /*-{
        $wnd.jQuery(document).ready(function()
        {
            $wnd.jQuery($wnd).focus(function()
            {
@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();               @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(true);
                $wnd.console.log("focus");
            }).blur(function()
            {
                var ret = false;
                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::focusVizsgalat()();
                                @com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(false);
                $wnd.console.log("blur");
            });
        });
    }-*/;
}

I see this in the browser console:

uncaught exception: java.lang.IllegalArgumentException: invoke arguments: JS value of type boolean, expected java.lang.Boolean

I've tested in Chrome and Firefox.

What is the problem?

Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-11-21 03:31:25

您必须将布尔值声明为原始布尔值才能从 javascript 设置值

,并且不需要在调用中指定 L/java/lang/Boolean,而是指定 Z

You have to declare the Boolean as the primitive boolean to set the value from javascript

and you dont need to specifiy L/java/lang/Boolean in the call but Z instead

请恋爱 2024-11-21 03:31:25

要么遵循 Daniel 的建议,但随后您必须更改方法以采用 boolean 参数(即始终使用 boolean),或者您可以显式强制转换/装箱您的JSNI 方法中 java.lang.Boolean 中的 boolean:

@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::valueOf(Z)(true));

...即使在您的情况下,因为该值是常量,我宁愿直接使用 Boolean 常量 TRUEFALSE

@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::TRUE);

话虽如此,我确实相信 Daniel 的建议最适合您。

Either follow Daniel's advice, but then you have to change your method to take a boolean argument (i.e. use boolean all the way through), or you can explicitly cast/box your boolean in a java.lang.Boolean in your JSNI method:

@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::valueOf(Z)(true));

…even though in your case, because the value is a constant, I'd rather directly use the Boolean constants TRUE and FALSE:

@com.eggproject_hu.WPECommerceAdminSales.client.AblakVillogo::setFocused(Ljava/lang/Boolean;)(@java.lang.Boolean::TRUE);

That being said, I do believe Daniel's advice is your best fit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文