簡體   English   中英

如何在Button上單擊JavaFX WebView調用JavaScript函數?

[英]How to call a JavaScript function from a JavaFX WebView on Button click?

我試圖在JavaFX按鈕單擊事件上從JavaFX WebView調用JavaScript函數。

我使用以下代碼,但它不起作用:

try {
  File file = new File("F:\\inputfile\\hello.htm");

  WebEngine webengine = webview.getEngine();
  webengine.load(file.toURI().toURL().toString());
} catch(Exception ex) {
  ex.printStackTrace();
}

在任何按鈕單擊我想在html文件中執行test() JavaScript方法:

webengine.executeScript("test()");

而html文件中的JavaScript方法是:

<script language="javascript">
  function test()
  {
    window.scrollBy(0, 20); 
  }
</script>

我使用你的代碼如下,它的工作原理

package org.im.oor;

import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/**
 *
 * @author maher
 */
public class main extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("fire JS");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if (webengine != null) 
                {
                    webengine.executeScript("test()");
                }
            }
        });

        publishServices();
        StackPane root = new StackPane();
//        root.getChildren().add(btn);
        HBox hh = new HBox();
        hh.getChildren().add(btn);
        hh.getChildren().add(webview);


        root.getChildren().add(hh);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    private WebEngine webengine;
    private static WebView webview;

    private void publishServices() {



        try {
            webview = new WebView();
            webview.setVisible(true);
            webengine = webview.getEngine();
            webengine.setJavaScriptEnabled(true);
            File file = new File("c:\\hello.html");
            System.out.println(file.exists() + " file exitence");
            webengine.load(file.toURI().toURL().toString());
        } catch (Exception ex) {
            System.err.print("error " + ex.getMessage());
            ex.printStackTrace();
        }




    }


    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

c:\\ hello.html的內容

<html>
<header>
<script language="javascript">
 function test()
 {
   //window.scrollBy(0,20); 
   document.body.style.backgroundColor="#00f3f3";
   alert('test');

 }
 </script>
</header>
<body>test
<a href='#' onclick="test();">fire for test</a>
</body>
</html>

檢查此代碼並讓我知道..只是一些點:

  • 檢查你是否真的可以訪問該文件..

  • 讓你的程序變得簡單,如果它正在運行JS <==> JavaFX,那么你可以進一步使用更高級的JS函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM