从intellij和maven运行JavaFX11项目会得到不同的结果

xv8emn3q  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(398)

我刚刚以Java11中IntellijMaven项目的形式开发了我的第一个javafx应用程序,并且达到了预期的效果。到目前为止,我只使用maven来处理依赖关系,使用intellij来编译和运行我的项目。
然后我继续尝试以jar文件的形式打包项目,发现由于java11不再包含javafx,jar生成变得更加困难(是的,我知道,我应该早点检查这个)。在阅读javafx11的文档时,我决定尝试使用maven生成jar文件,而不是使用intellij的artifact build(对于javafx11项目,它甚至可能不再工作)。
因此,我在pom.xml文件中添加了所需的依赖项,最著名的是javafx插件和javafx库(到目前为止,intellij正在从我在项目设置中指定的目录加载javafx,如itellij的文档中所述)。
在尝试实际构建jar包之前,我检查了是否能够使用javafx:run goal 我的应用程序在用maven编译时看起来与用intellij编译的版本不同:
正确的intellij版本

maven版本错误

最值得注意的是,它似乎没有加载菜单底部的图像,但我没有得到任何关于加载该图像的警告或错误。另外,我可以通过在窗口上移动鼠标光标来完全显示按钮,但是图像永远不会显示,图像框也不会移动到正确的位置。
所讨论的映像与所有其他资源一起位于src/main/resources中(这应该是maven的默认资源路径,其他资源似乎已正确加载)。我能找到的唯一区别是,我正在从java代码加载所有其他资源,而该映像加载在一个fxml文件中,该文件与映像位于同一个resources目录中。
为了完成我的问题,我把它放在这里
在我的main.class文件中,我正在创建您在屏幕截图中看到的菜单:

package sample;

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

import java.io.File;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main extends Application {
    public static Stage stage;
    private final static Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    private static String tempDir;
    private final static String projectName="ReportManager";

    @Override
    public void start(Stage primaryStage) throws Exception{ //https://stackoverflow.com/questions/26325403/how-to-implement-language-support-for-javafx-in-fxml-documents
        if(getClass().getResource("/sample.fxml")!=null){
            ResourceBundle languageResourceBundle = ResourceBundle.getBundle("languageResources"); //Loads bundle for the default JMV locale
            Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"), languageResourceBundle);
            primaryStage.setTitle("ReportManager");
            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/bar-chart-2.png"))); //Sets the icon for the window
            primaryStage.setScene(new Scene(root)); //If I don't specify width and height it auto-resizes using values from fxml file
            this.stage=primaryStage;
            primaryStage.show();
        }
        else{
            System.err.println("Can't start GUI: got null instead of fxml file");
            logger.severe("Can't start GUI: got null instead of fxml file");
            return;
        }
    }

    public static String getTempDir() {
        return tempDir;
    }

    public static void main(String[] args) {
        logger.setLevel(Level.WARNING);
        tempDir=System.getProperty("java.io.tmpdir") + File.separator + projectName;
        if(!new File(tempDir).mkdir()){
            logger.info("Failed to create temporary directory, maybe it already exists?");
        }
        launch(args);
    }
}

我用来生成菜单的fxml文件:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>

<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="354.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="creaReportButton" alignment="CENTER" mnemonicParsing="false" onAction="#avviaReportApplication" text="%generateReport" />
      <Region prefHeight="19.0" prefWidth="400.0" />
      <Button fx:id="registraReportButton" alignment="CENTER" mnemonicParsing="false" onAction="#registraSullaBlockchain" text="%registerDocument" />
      <Region prefHeight="19.0" prefWidth="400.0" />
      <Button mnemonicParsing="false" onAction="#verificaReportSullaBlockchain" text="%verifyDocument" />
      <Region layoutX="10.0" layoutY="36.0" prefHeight="19.0" prefWidth="400.0" />
      <Button mnemonicParsing="false" onAction="#settings" text="%settings" />
      <Region layoutX="10.0" layoutY="126.0" prefHeight="51.0" prefWidth="400.0" />
      <ImageView fitHeight="135.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@logo.jpg" />
         </image></ImageView>
   </children>
</VBox>

我的pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>Interface</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.5</version>
                <configuration>
                    <mainClass>sample.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
    <dependency>
    <groupId>com.eternitywall</groupId>
    <artifactId>java-opentimestamps</artifactId>
    <version>1.19</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
            </exclusion>
        </exclusions>
</dependency>

        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.15</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.10.19</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.19</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j -->
        <dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>2.6.0</version>
        </dependency>

       <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>12.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>12.0.2</version>
        </dependency>

    </dependencies>

</project>

我做错什么了?感谢所有帮助我的人。

2vuwiymt

2vuwiymt1#

刚刚复制了我的:

<dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-base</artifactId>
        <version>13.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>13.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>13.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>13.0.2</version>
    </dependency>

将版本调整为12。不使用webview时可能不需要javafxweb。我猜:图形。
因此,答案很简单:vbox可以使用 setFillWidth(true) 以及 setPadding(new Insets(10))

相关问题