2015-10-20 321 views
0

我正在使用JavaFX嵌入浏览器。我试图从java类WebScale运行一个JavaScript函数addnum(),但我得到错误。如果我从webengine.executeScript()执行document.write()它是可能的。但我不能打电话给我的功能。 我的代码如下:JavaFx无法执行javascript函数

public class WebScale extends JApplet { 
static ZoomingPane zoomingPane; 
private static JFXPanel fxContainer; 

/** 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      final JFrame frame = new JFrame("Area Configurator"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      JApplet applet = new WebScale(); 
      applet.init(); 

      frame.setContentPane(applet.getContentPane()); 

      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 

      applet.start(); 

      frame.addComponentListener(new java.awt.event.ComponentAdapter() { 
       @Override 
       public void componentResized(java.awt.event.ComponentEvent evt) { 
        if (zoomingPane != null) { 
         zoomingPane.setZoomFactors((double)(frame.getWidth()/ 1280.0), (double)(frame.getHeight()/800.0)); 
        } 
       } 
      }); 
     } 
    }); 
} 

@Override 
public void init() { 
    fxContainer = new JFXPanel(); 
    fxContainer.setPreferredSize(new Dimension(800, 700)); 
    add(fxContainer, BorderLayout.CENTER); 
    // create JavaFX scene 
    Platform.runLater(new Runnable() { 

     @Override 
     public void run() { 
      createScene(); 
     } 
    }); 
} 

private void createScene() { 
    WebView webView = new WebView(); 
    zoomingPane = new ZoomingPane(webView); 
    BorderPane bp = new BorderPane(); 
    bp.setCenter(zoomingPane); 
    fxContainer.setScene(new Scene(bp)); 
    String strpath ; 
    strpath="file:///C:/Users/Priyanka/Desktop/FDASH/StationV3/Main.html"; 


    final WebEngine engine = webView.getEngine(); 

    engine.load(strpath); 

    engine.executeScript("addNum()"); 



} 

private class ZoomingPane extends Pane { 
    Node content; 
    private final DoubleProperty zoomFactor = new SimpleDoubleProperty(1); 
    private double zoomFactory = 1.0; 

    private ZoomingPane(Node content) { 
     this.content = content; 
     getChildren().add(content); 
     final Scale scale = new Scale(1, 1); 
     content.getTransforms().add(scale); 

     zoomFactor.addListener(new ChangeListener<Number>() { 
      @Override 
      public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) { 
       scale.setX(newValue.doubleValue()); 
       scale.setY(zoomFactory); 
       requestLayout(); 
      } 
     }); 
    } 

    @Override 
    protected void layoutChildren() { 
     Pos pos = Pos.TOP_LEFT; 
     double width = getWidth(); 
     double height = getHeight(); 
     double top = getInsets().getTop(); 
     double right = getInsets().getRight(); 
     double left = getInsets().getLeft(); 
     double bottom = getInsets().getBottom(); 
     double contentWidth = (width - left - right)/zoomFactor.get(); 
     double contentHeight = (height - top - bottom)/zoomFactory; 
     layoutInArea(content, left, top, 
       contentWidth, contentHeight, 
       0, null, 
       pos.getHpos(), 
       pos.getVpos()); 
    } 

    public final Double getZoomFactor() { 
     return zoomFactor.get(); 
    } 
    public final void setZoomFactor(Double zoomFactor) { 
     this.zoomFactor.set(zoomFactor); 
    } 
    public final void setZoomFactors(Double zoomFactorx, Double Zoomfactory) { 
     this.zoomFactory = Zoomfactory; 
     this.zoomFactor.set(zoomFactorx); 
    } 

    public final DoubleProperty zoomFactorProperty() { 
     return zoomFactor; 
    } 
} 

}

我收到以下错误。

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: ReferenceError: Can't find variable: addNum 
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:128) 
at com.sun.webkit.WebPage.twkExecuteScript(Native Method) 
at com.sun.webkit.WebPage.executeScript(WebPage.java:1439) 
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:982) 
at c.WebScale.createScene(WebScale.java:97) 
at c.WebScale.access$0(WebScale.java:83) 
at c.WebScale$2.run(WebScale.java:78) 
at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:745) 
+0

addNum()定义在哪里?你能分享它的代码吗? – Amber

回答

1

假设addNum()在main.html中定义的JavaScript还没有为你调用它的时候加载。您应该添加一个监听器,以便在页面完全加载后调用JavaScript:

final WebEngine engine = webView.getEngine(); 
    engine.getLoadWorker().stateProperty().addListener(
      new ChangeListener<State>() { 
       public void changed(ObservableValue ov, State oldState, State newState) { 
        if (newState == State.SUCCEEDED) { 
         engine.executeScript("addNum()"); 
        } 
       } 
      }); 

    engine.load(strpath); 
+0

谢谢。它为我工作。 – Priya