【发布时间】:2015-06-02 04:55:44
【问题描述】:
我可以使用 JavaFX WebEngine 和 WebView 成功地打开一个显示 html 文档/url 的 WebView,但我想在 FXML 文档中打开一个 WebView 以及按钮和 ImageView 等其他元素。我的目标是有一个 GUI 来显示图像,其中包含一个显示在 WebView 对象内的 html 文档。
这是自己打开 WebView 的程序:
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
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.
}
}
WebViewController.java
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();
}
}
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
我的目标可以实现吗?如果是这样有没有人知道这样做的方法?如有任何帮助,我将不胜感激,谢谢。
【问题讨论】:
-
你的自定义 WebView 和 FXML 中的 WebView 同名,但它们是完全不同的类。
-
@Prometheus 感谢您的评论,我认为这些类仍然应该能够相互交互,因为我之前做过完全相同的事情,但使用的是 TextArea 而不是 WebView。我的主要关注点是控制器类。我想我在这里问的问题更好:stackoverflow.com/questions/29353547/…