2

我想在 DJ 原生 swing jwebbrowser 中执行我的 javascript 代码,但我应该等待页面加载,如何设置监听器?我尝试以下操作,但加载后总是运行脚本,它也运行得很晚!

webBrowser.addWebBrowserListener(new WebBrowserListener(){

        @Override
        public void commandReceived(WebBrowserCommandEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void loadingProgressChanged(WebBrowserEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void locationChangeCanceled(WebBrowserNavigationEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void locationChanged(WebBrowserNavigationEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void locationChanging(WebBrowserNavigationEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void statusChanged(WebBrowserEvent arg0) {
            // TODO Auto-generated method stub
            if(webBrowser.getLoadingProgress() == 100)
                webBrowser.executeJavascript("some js code");");
        }

        @Override
        public void titleChanged(WebBrowserEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowClosing(WebBrowserEvent arg0) {
            // TODO Auto-generated method stub
            frame.dispose();
        }

        @Override
        public void windowOpening(WebBrowserWindowOpeningEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void windowWillOpen(WebBrowserWindowWillOpenEvent arg0) {
            // TODO Auto-generated method stub

        }

    });

我该怎么办?

4

1 回答 1

2

我应该覆盖 loadingProgressChanged(WebBrowserEvent arg0) 而不是 statusChanged(WebBrowserEvent arg0)

@Override public void loadingProgressChanged(WebBrowserEvent arg0) { // TODO Auto-generated method stub if(webBrowser.getLoadingProgress() == 100) webBrowser.executeJavascript("some js code"); }

但是加载我的html仍然需要时间!

于 2014-07-18T08:05:43.170 回答