html 按钮代号一的处理事件

Handle event for html button codename one

为了丰富 ui,我在表单中嵌入了包含大量 html 内容(主要是 Table、按钮)的 WebBrowser 组件。是否可以在单击 html 按钮时处理事件?

当然,请查看厨房水槽演示以获取示例。

通常只需导航到事件中的 URL 并实现您自己的 BrowserNavigationCallback 来处理到特定 URL 的导航。

这是 Kitchen Sink 演示中的代码,请注意 setBrowserNavigationCallback 块:

    final WebBrowser wb = new WebBrowser();
    if(wb.getInternal() instanceof BrowserComponent) {
        Button btn = new Button("Add");
        final TextArea content = new TextArea();
        Container north = new Container(new BorderLayout());
        north.addComponent(BorderLayout.CENTER, content);
        north.addComponent(BorderLayout.EAST, btn);
        cnt.addComponent(BorderLayout.NORTH, north);
        content.setHint("Add to web document");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                ((BrowserComponent)wb.getInternal()).execute("fnc('" + content.getText() + "');");
            }
        });
        ((BrowserComponent)wb.getInternal()).setBrowserNavigationCallback(new BrowserNavigationCallback() {
            public boolean shouldNavigate(String url) {
                if(url.startsWith("http://sayhello")) {
                    // warning!!! This is not on the EDT and this method MUST return immediately!
                    Display.getInstance().callSerially(new Runnable() {
                        public void run() {
                            ((BrowserComponent)wb.getInternal()).execute("fnc('this was written by Java code!')");
                        }
                    });
                    return false;
                }
                return true;
            }
        });
    }