JavaFX FXML:加载后空白WebView

时间:2022-11-18 17:01:25

When I try to load any html or urls using webEngine.load(); my webView is just blank. From what I have read here "JavaFX 2.2 WebView" it seems like I will have to sign my application to let it run outside of sandbox mode. http://docs.oracle.com/javafx/2/deployment/deploy_overview.htm#CEGJGHDA

当我尝试使用webEngine.load()加载任何html或url时;我的webView只是空白。从我在这里阅读的“JavaFX 2.2 WebView”看来,我似乎必须签署我的应用程序才能让它在沙盒模式之外运行。 http://docs.oracle.com/javafx/2/deployment/deploy_overview.htm#CEGJGHDA

Is that what is causing this problem?

是什么导致了这个问题?

I am using NetBeans 8.1, and under Project Settings I am running it as Standalone. I have been following these tutorials and each has gone well. http://docs.oracle.com/javase/8/javafx/get-started-tutorial/get_start_apps.htm#JFXST804

我使用的是NetBeans 8.1,在Project Settings下我将它作为Standalone运行。我一直在关注这些教程,每个教程都很顺利。 http://docs.oracle.com/javase/8/javafx/get-started-tutorial/get_start_apps.htm#JFXST804

Here are my three files.

这是我的三个文件。

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.media.*?>
<?import javafx.scene.web.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="481.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <AnchorPane prefHeight="18.0" prefWidth="600.0">
         <children>
            <HBox layoutX="157.0" layoutY="14.0" prefHeight="64.0" prefWidth="287.0">
               <children>
                  <Label text="TwitchAid">
                     <font>
                        <Font size="53.0" />
                     </font>
                  </Label>
                  <ImageView fitHeight="150.0" fitWidth="38.0" pickOnBounds="true" preserveRatio="true">
                     <image>
                        <Image url="@Twitchaid-Logo.png" />
                     </image>
                  </ImageView>
               </children>
            </HBox>
         </children>
      </AnchorPane>
      <AnchorPane prefHeight="200.0" prefWidth="200.0">
         <children>
            <WebView fx:id="webView" prefHeight="405.0" prefWidth="600.0" />
         </children>
      </AnchorPane>
   </children>
</VBox>

Java

/*
 * 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 twitchauthorize;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Dylan
 */
public class TwitchAuthorize extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLTwitchAuthorize.fxml"));
        Scene scene = new Scene(root);   
        stage.setScene(scene);
        stage.show();

        stage.setResizable(false);
    }

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

}

Controller.java

/*
 * 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 twitchauthorize;

import javafx.fxml.FXML;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

/**
 *
 * @author Dylan
 */
public class FXMLTwitchAuthorizeController {

    @FXML
    private WebView webView;

    @FXML
    private void initialize(){
        WebEngine engine = webView.getEngine();
        engine.load("http://www.google.com");
    }

}

1 个解决方案

#1


1  

You haven't specified a controller in your FXML, so the initialize method of the controller is never executed.

您尚未在FXML中指定控制器,因此永远不会执行控制器的初始化方法。

Add the following attribute definition to the VBox element that forms the root element of your FXML:

将以下属性定义添加到构成FXML根元素的VBox元素:

fx:controller="twitchauthorize.FXMLTwitchAuthorizeController" 

#1


1  

You haven't specified a controller in your FXML, so the initialize method of the controller is never executed.

您尚未在FXML中指定控制器,因此永远不会执行控制器的初始化方法。

Add the following attribute definition to the VBox element that forms the root element of your FXML:

将以下属性定义添加到构成FXML根元素的VBox元素:

fx:controller="twitchauthorize.FXMLTwitchAuthorizeController"