JavaFX JSObject не может ссылаться на класс Java, связанный с HTML - PullRequest
0 голосов
/ 24 октября 2018

Когда я пытаюсь запустить свой проект Java (который отлично работает на моем ноутбуке), функция app.createFile () в моем HTML не может вызвать функцию на моей странице 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

Мой HTML со свойством "app"

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

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

</body>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...