FXML文档中的JavaFX打开WebView

FXML文档中的JavaFX打开WebView,java,webview,javafx,fxml,Java,Webview,Javafx,Fxml,我可以使用JavaFX WebEngine和WebView成功地打开一个显示html文档/url的WebView,但我想在FXML文档中打开一个WebView,以及其他元素,如按钮和ImageView。我的目标是拥有一个GUI,它可以在WebView对象中显示带有html文档的图像 下面是一个可以自己打开网络视图的程序: public class HTMLViewer extends Application { private Scene scene; MyBrowser my

我可以使用JavaFX WebEngine和WebView成功地打开一个显示html文档/url的WebView,但我想在FXML文档中打开一个WebView,以及其他元素,如按钮和ImageView。我的目标是拥有一个GUI,它可以在WebView对象中显示带有html文档的图像

下面是一个可以自己打开网络视图的程序:

public class HTMLViewer extends Application {

    private Scene scene;
    MyBrowser myBrowser;
    TextArea myTextArea;


public static void main(String[] args) throws IOException, BadLocationException {

    launch(args);
}

    @Override
    public void start(Stage primaryStage) {
    primaryStage.setTitle("HTMLViewer");

    myBrowser = new MyBrowser();
    scene = new Scene(myBrowser, 800, 600);

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

class MyBrowser extends Region{

    final String hellohtml = "chang.htm"; //html file to view

    WebView webView = new WebView();
    WebEngine webEngine = webView.getEngine();

    public MyBrowser(){

        URL urlHello = getClass().getResource(hellohtml); 
        webEngine.load(urlHello.toExternalForm());

        getChildren().add(webView);
    }
  }    
}
下面是我通过FXML文档加载WebView的尝试,该文档需要WebView.java主类、WebViewController.java和WebView.FXML

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

<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="webview.WebViewController">
   <children>
      <WebView fx:id="webView" layoutX="100.0" layoutY="176.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>
WebView.java

public class WebView extends Application {
private Object primaryStage;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws IOException {
    Parent root =    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    primaryStage.setTitle("HTMLViewer");
    Scene scene = new Scene(root);

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



WebEngine getEngine() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
public class FXMLDocumentController implements Initializable {

@FXML
private Label label;
//WebView mywebview = new WebView();
String link = "https://www.google.com";
WebEngine engine;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@FXML WebView mywebview; //access WebView in FXML document
public void displayWeb() {
    WebEngine engine = mywebview.getEngine();
    final String hellohtml = "chang.htm"; //HTML file to view in web view

    URL urlHello = getClass().getResource(hellohtml);
    engine.load(urlHello.toExternalForm());
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    engine = mywebview.getEngine();
    engine.load(link);
    displayWeb();
}    

}
package webview;

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

public class Main extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("WebView.fxml"));
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch();
    }
}
package webview;

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

public class WebViewController
{
    @FXML
    private WebView webView;

    @FXML
    private void initialize()
    {
        WebEngine engine = webView.getEngine();
        engine.load("http://www.example.org");
    }
}
WebViewController.java

public class WebView extends Application {
private Object primaryStage;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws IOException {
    Parent root =    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    primaryStage.setTitle("HTMLViewer");
    Scene scene = new Scene(root);

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



WebEngine getEngine() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
public class FXMLDocumentController implements Initializable {

@FXML
private Label label;
//WebView mywebview = new WebView();
String link = "https://www.google.com";
WebEngine engine;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@FXML WebView mywebview; //access WebView in FXML document
public void displayWeb() {
    WebEngine engine = mywebview.getEngine();
    final String hellohtml = "chang.htm"; //HTML file to view in web view

    URL urlHello = getClass().getResource(hellohtml);
    engine.load(urlHello.toExternalForm());
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    engine = mywebview.getEngine();
    engine.load(link);
    displayWeb();
}    

}
package webview;

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

public class Main extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("WebView.fxml"));
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch();
    }
}
package webview;

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

public class WebViewController
{
    @FXML
    private WebView webView;

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

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

<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="900.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="webview.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="23.0" layoutY="24.0" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <WebView fx:id="mywebview" layoutX="102.0" layoutY="20.0" prefHeight="559.0" prefWidth="775.0" />
    </children>
</AnchorPane>

运行应用程序时出现异常

无法将webview.webview字段webview.WebViewController.mywebview设置为javafx.scene.web.webview


我的目标可能实现吗?如果有,有人知道这样做的方法吗?如果有任何帮助,我将不胜感激,谢谢。

从FXML将WebView加载到控制器时,它的工作原理与任何其他控件一样。下面是一个显示网站的简单示例程序

Main.java

public class WebView extends Application {
private Object primaryStage;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws IOException {
    Parent root =    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    primaryStage.setTitle("HTMLViewer");
    Scene scene = new Scene(root);

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



WebEngine getEngine() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
public class FXMLDocumentController implements Initializable {

@FXML
private Label label;
//WebView mywebview = new WebView();
String link = "https://www.google.com";
WebEngine engine;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@FXML WebView mywebview; //access WebView in FXML document
public void displayWeb() {
    WebEngine engine = mywebview.getEngine();
    final String hellohtml = "chang.htm"; //HTML file to view in web view

    URL urlHello = getClass().getResource(hellohtml);
    engine.load(urlHello.toExternalForm());
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    engine = mywebview.getEngine();
    engine.load(link);
    displayWeb();
}    

}
package webview;

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

public class Main extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("WebView.fxml"));
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch();
    }
}
package webview;

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

public class WebViewController
{
    @FXML
    private WebView webView;

    @FXML
    private void initialize()
    {
        WebEngine engine = webView.getEngine();
        engine.load("http://www.example.org");
    }
}
WebViewController.java

public class WebView extends Application {
private Object primaryStage;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) throws IOException {
    Parent root =    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    primaryStage.setTitle("HTMLViewer");
    Scene scene = new Scene(root);

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



WebEngine getEngine() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
public class FXMLDocumentController implements Initializable {

@FXML
private Label label;
//WebView mywebview = new WebView();
String link = "https://www.google.com";
WebEngine engine;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@FXML WebView mywebview; //access WebView in FXML document
public void displayWeb() {
    WebEngine engine = mywebview.getEngine();
    final String hellohtml = "chang.htm"; //HTML file to view in web view

    URL urlHello = getClass().getResource(hellohtml);
    engine.load(urlHello.toExternalForm());
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    engine = mywebview.getEngine();
    engine.load(link);
    displayWeb();
}    

}
package webview;

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

public class Main extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        Parent root = FXMLLoader.load(getClass().getResource("WebView.fxml"));
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args)
    {
        launch();
    }
}
package webview;

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

public class WebViewController
{
    @FXML
    private WebView webView;

    @FXML
    private void initialize()
    {
        WebEngine engine = webView.getEngine();
        engine.load("http://www.example.org");
    }
}
我没有在这里使用,因为它在JavaFX8中有些过时

此接口已被位置和资源属性自动注入控制器所取代。FXMLLoader现在将自动调用控制器定义的任何带适当注释的no arg initialize()方法。建议尽可能使用注入法

WebView.fxml

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

<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="webview.WebViewController">
   <children>
      <WebView fx:id="webView" layoutX="100.0" layoutY="176.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>


为什么这在你的控制器中不起作用?因为您导入了错误的WebView。在控制器中,webView变量是自定义webView.webView类的实例,该类扩展了应用程序而不是控件。FXMLLoader试图从FXML文件中分配javafx.scene.web.WebView,但失败,因为类型不兼容。

您的自定义WebView和FXML中的WebView同名,但它们是完全不同的类。@Prometheus感谢您的评论,我认为这些类应该仍然能够相互交互,因为我以前做过完全相同的事情,但是使用了TextArea而不是WebView。我主要关注的是控制器类。我想我在这里问了一个更好的问题:这对我不起作用,我的webView在运行项目后只是一个空白框。有什么帮助吗?这不起作用,因为您正在尝试加载https站点。以下链接可能对您有所帮助。