【发布时间】:2021-06-21 16:26:00
【问题描述】:
我使用我在此处找到的原型创建了一个 Maven JavaFX 项目:https://github.com/openjfx/javafx-maven-archetypes
一切运行顺利,我可以运行一个带有一些标签、一个文本框和一些按钮的项目而不会出现问题。但是,当我尝试添加 WebView 元素时(尽管 IntelliJ 中的 Scene Builder 可以识别),我无法让导入工作。
我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled3</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.openjfx/javafx-web -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>16</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>org.dianavrabie.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的主要 fxml 文档来描述视图:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox alignment="TOP_CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.dianavrabie.PrimaryController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
<children>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Label layoutX="14.0" layoutY="14.0" text="POLYNOMIAL CALCULATOR" AnchorPane.leftAnchor="0.0" AnchorPane.topAnchor="0.0">
<font>
<Font name="System Bold" size="36.0" />
</font></Label>
<Label layoutY="53.0" text="Polynomial 1" AnchorPane.leftAnchor="0.0">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutY="90.0" text="Polynomial 2" AnchorPane.leftAnchor="0.0">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField layoutX="124.0" layoutY="54.0" prefHeight="25.0" prefWidth="359.0" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="0.0" />
<TextField layoutX="117.0" layoutY="91.0" prefHeight="25.0" prefWidth="359.0" AnchorPane.leftAnchor="117.0" AnchorPane.rightAnchor="0.0" />
<Button layoutY="136.0" mnemonicParsing="false" text="ADD" AnchorPane.leftAnchor="0.0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
<Button layoutX="57.0" layoutY="136.0" mnemonicParsing="false" text="SUBTRACT" AnchorPane.leftAnchor="57.0">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
<Button layoutX="156.0" layoutY="136.0" mnemonicParsing="false" text="MULTIPLY">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
<Button layoutX="251.0" layoutY="137.0" mnemonicParsing="false" text="DIVIDE">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
<Button layoutX="324.0" layoutY="137.0" mnemonicParsing="false" text="DERIVATIVE">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
<Button layoutX="432.0" layoutY="137.0" mnemonicParsing="false" text="INTEGRATE">
<font>
<Font name="System Bold" size="14.0" />
</font>
</Button>
<Button layoutX="538.0" layoutY="137.0" mnemonicParsing="false" text="CLEAR" textFill="#fc5400">
<font>
<Font size="14.0" />
</font>
</Button>
</children>
</AnchorPane>
</children>
</VBox>
还有主应用类
package org.dianavrabie;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
/**
* JavaFX App
*/
public class App extends Application {
private static Scene scene;
@Override
public void start(Stage stage) throws IOException {
scene = new Scene(loadFXML("primary"), 640, 480);
stage.setScene(scene);
stage.show();
}
static void setRoot(String fxml) throws IOException {
scene.setRoot(loadFXML(fxml));
}
private static Parent loadFXML(String fxml) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
return fxmlLoader.load();
}
public static void main(String[] args) {
launch(args);
}
}
我需要做什么才能让 WebView 工作?我对此很陌生,似乎找不到解决方案。
【问题讨论】:
-
您好!它可能没有用,但是您是否尝试使用相同的 JavaFx 组合版本?您使用 Java 版本 11,Javafx 版本 13 用于控制,fxml 和 Javafx 版本 16 用于 javafx web...我会尝试使用同质版本以避免冲突。
-
我在添加 WebView 时也遇到了问题,但我有一个异常说“模块 javafx.graphics 不会将 com.sun.javafx.sg.prism 导出到未命名的模块”(以及一堆错误同类型)。它可以通过添加“--add-exports javafx.graphics/com.sun.prism=ALL-UNNAMED”(以及每个错误的许多其他导出)来解决,但是添加所有这些很奇怪,我一定遗漏了一些东西.