JavaFX JSObject无法链接到Java类

时间:2018-10-24 12:26:19

标签: java html javafx webview javafx-webengine

当我尝试运行Java项目(可在笔记本电脑上完美运行)时,HTML中的app.createFile()函数无法在JavaLink页面中调用该函数。我很确定它位于已安装的软件包中。我使用NetBeans进行开发,并且已经尝试重新安装Java JRE和NetBeans

Main.java

这是我创建网络引擎并为HTML页面“ app”设置成员的类。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication2;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
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.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;

/**
 *
 * @author cooki_000
 */
public class JavaFXApplication2 extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {


        String PATH = "";
        primaryStage.setTitle("WebView App");

        WebView browser = new WebView();
        WebEngine engine = browser.getEngine();
        String cwd = System.getProperty("user.dir");
        String name = "\\src\\web\\index.html";

        engine.loadContent(getPATHtoHTML(cwd + name));

        JSObject win = (JSObject) engine.executeScript("window");
        win.setMember("app", new JavaLink());

        StackPane sp = new StackPane();
        sp.getChildren().add(browser);

        Scene root = new Scene(sp);

        primaryStage.setScene(root);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private String getPATHtoHTML(String path) throws FileNotFoundException, IOException {
        String content = null;
        BufferedReader br = null;
        FileReader fr = null;

        fr = new FileReader(path);
        br = new BufferedReader(fr);
        String nextLine = "";
        StringBuilder sb = new StringBuilder();
        while ((nextLine = br.readLine()) != null) {
            sb.append(nextLine);
        }
        content = sb.toString();
        return content;
    }

    private void changeHTML(String stringToAdd){

    }

}

JavaLink.java

这是我将从HTML文件中调用的方法的类。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplication2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 *
 * @author cooki_000
 */
public class JavaLink {
    public void createFile() throws FileNotFoundException, IOException {
        System.out.println("Création d'un fichier de preuve");

        String cwd = System.getProperty("user.dir");

        File file = new File(cwd + "\\preuve.txt");

          FileWriter writer = new FileWriter(file);
          writer.write("Why are you reading this?");
          writer.close();
    }
}

index.html

具有“ app”属性的HTML

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<input type="button" value="Cliquez moi" onclick="app.createFile()"/>

</body>
</html>

0 个答案:

没有答案