Browser.evaluate()方法显示错误

时间:2014-08-03 10:46:27

标签: java javascript eclipse swt

我使用SWT API编写了一个简单的Java程序。我在应用程序中实例化了一个浏览器并尝试使用browser.evaluate()方法。但它显示以下错误

类型浏览器

的方法evaluate(String)未定义

这是我完整的程序

    import java.awt.event.MouseEvent;

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.SWTError;
    import org.eclipse.swt.custom.CTabFolder;
    import org.eclipse.swt.custom.CTabItem;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.browser.*;
    import org.eclipse.swt.printing.*;
    import org.eclipse.swt.events.*;



    public class example {

     public static void main(String args[])
     {
        final Display display = new Display();
        final Shell shell=new Shell(display);
        shell.setText("Informatica Business Glossary Desktop");

        final Browser browser = new Browser(shell,SWT.NONE);

        final String SCRIPT01 = "var html = \"\";"+
                "if (typeof window.getSelection != \"undefined\") {"+
                    "var sel = window.getSelection();"+
                    "if (sel.rangeCount) {"+
                        "var container = document.createElement(\"div\");"+
                        "for (var i = 0, len = sel.rangeCount; i < len; ++i) {"+
                            "container.appendChild(sel.getRangeAt(i).cloneContents());"+
                        "}"+
                        "html = container.innerHTML;"+
                    "}"+
                "} else if (typeof document.selection != \"undefined\") {"+
                    "if (document.selection.type == \"Text\") {"+
                        "html = document.selection.createRange().htmlText;"+
                    "}" +
                "}" +
                "return html";
        //final Browser browser;


    if(browser!=null)
{

    browser.setUrl("https://www.google.com");
}

        browser.addMouseListener(new MouseListener(){

            @Override
            public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
                String selection = (String)browser .evaluate(SCRIPT01);
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseDown(org.eclipse.swt.events.MouseEvent e) {

                // TODO Auto-generated method stub

            }

            @Override
            public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
                String selection = (String) browser.evaluate(SCRIPT01);
                // TODO Auto-generated method stub

            }



        });
     }  
    }

错误显示在

String selection = (String) browser.evaluate(SCRIPT01);  

我正在使用Eclipse Juno

1 个答案:

答案 0 :(得分:1)

evaluate类的Browser方法的JavaDoc表示它已添加到SWT版本3.5中,因此您至少需要拥有该版本。

Eclipse Luna的最新版本是here