【问题标题】:JavaFX WebView under IDEAIDEA下的JavaFX WebView
【发布时间】:2020-05-20 14:38:58
【问题描述】:

我是 IDEA 的新手。我在 Windows 10 下使用 JDK 11.0.2 和 javafx-sdk 11.0.2 运行版本 Community 2020.1 和 Gluon SceneBuilder 11.0.1。 感谢 Egor Klepikov (IntelliJ),我编写了一个小型测试程序,需要添加很多运行配置。这是控制器的代码:

public class Controller {
    public Button btn1;
    public WebView webView = new WebView();
    WebEngine webEngine = webView.getEngine();
    public Button showBtn;
    String fpath;

    @FXML
    public void initialize() {
//        System.out.println("in controller initialize");
        File file = new File(String.valueOf(getClass().getResource("RateCalculatorHelp.html")));
        fpath = file.getPath();
//        System.out.println("Path: " + fpath);
        webEngine.load(fpath);
   }

    public void showHtml(ActionEvent actionEvent) {
        webView.setVisible(true);
     }
}

当我运行应用程序时,没有错误消息,但我的 html 文件没有显示。我不知道为什么!欢迎帮助。

PS 有没有办法将项目的文件附加到我的消息中?

【问题讨论】:

标签: javafx


【解决方案1】:

我从 Egor Klepikov (IntelliJ) 那里得到了答案。 webEngine 变量初始化被移动到控制器的 initialize() 方法中,并且应用程序将工作! 这是修改后的代码:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import java.io.File;

public class Controller {
    public Button btn1;
    public WebView webView; // = new WebView();
    WebEngine webEngine; // = webView.getEngine();
    public Button showBtn;
    String fpath;

    @FXML
    public void initialize() {
//        System.out.println("in controller initialize");
        webEngine = webView.getEngine();
        File file = new File(String.valueOf(getClass().getResource("RateCalculatorHelp.html")));
        fpath = file.getPath();
//        System.out.println("Path: " + fpath);
        webEngine.load(fpath);
   }

    public void showHtml(ActionEvent actionEvent) {
        webView.setVisible(true);
     }
}

正如我在最初的消息中提到的,我必须在 IDEA 运行配置的 VM 选项中添加很多内容。这是列表:

--module-path
D:\JavaFX\javafx-sdk-11.0.2\lib
--add-modules
javafx.controls,javafx.fxml
--add-exports
javafx.graphics/com.sun.javafx.sg.prism=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.util=ALL-UNNAMED
--add-exports
javafx.base/com.sun.javafx.logging=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.prism=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.glass.ui=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.geom.transform=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.glass.utils=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.font=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.application=ALL-UNNAMED
--add-exports
javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.scene.input=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.geom=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.prism.paint=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.scenario.effect=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.text=ALL-UNNAMED
--add-exports
javafx.graphics/com.sun.javafx.iio=ALL-UNNAMED

我意识到这是因为 JavaFX 从版本 11 开始不再是 JDK 的一部分。我希望将来有人可以让使用 javaFX 变得更加实用!

【讨论】:

    猜你喜欢
    • 2015-03-22
    • 2016-06-13
    • 1970-01-01
    • 2016-11-18
    • 2021-05-03
    • 1970-01-01
    • 2013-10-09
    • 2019-05-04
    • 2015-08-14
    相关资源
    最近更新 更多