当前位置: 首页 > 知识库问答 >
问题:

从Intellij和Maven运行JavaFX 11项目会产生不同的结果

王兴庆
2023-03-14

我刚刚在Java11以IntelliJ Maven项目的形式开发了我的第一个JavaFX应用程序,并达到了它按预期工作的地步。到目前为止,我只使用Maven来处理依赖关系,使用IntelliJ来编译和运行我的项目。
然后我继续尝试以JAR文件的形式打包项目,发现由于Java11不再包含JavaFX,JAR生成变得更加困难(是的,我知道,我应该早点检查这个)。在阅读JavaFX 11的文档时,我决定尝试使用Maven生成JAR文件,而不是使用IntelliJ的工件构建(这可能不再适用于JavaFX 11项目)。
所以我在我的pom.xml文件中添加了所需的依赖项,最显着的是JavaFX插件和JavaFX库(到目前为止,IntelliJ正在从我在项目设置中指定的目录加载JavaFX,如ItelliJ的文档中所述)。
在尝试实际构建我的JAR包之前,我检查了我是否能够使用JavaFX插件的javafx: run目标编译和运行项目,最后我们找到了我的问题:我的应用程序在使用Maven编译时看起来与使用Maven编译的版本不同IntelliJ:

正确的IntelliJ版本

Maven版本错误

最值得注意的是,它似乎没有在菜单底部加载图像,但我没有收到任何关于加载该图像的警告或错误。此外,我可以通过在窗口上移动鼠标光标来使按钮完全显示,但图像永远不会显示,图像框也不会移动到正确的位置。
有问题的图像与所有其他资源一起位于src/main/资源中(这应该是Maven的默认资源路径,并且其他资源似乎已正确加载)。我能找到的唯一区别是,我正在从Java代码中加载所有其他资源,而该图像加载在FXML文件中,该文件与图像位于相同的资源目录中。
为了完成我的问题,我将把它放在这里

  • 我的Main。类文件,我将在其中创建您在屏幕截图中看到的菜单:
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>

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

共有1个答案

萧建木
2023-03-14

刚刚复制了我的:

    <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时,可能不需要javafx web。我的猜测是:图形。

因此,答案很简单:VBox可以使用设置填充宽度(true)和设置填充(新的插入(10))

 类似资料:
  • 我们可以看到相应的消息。此消息用作axios的警报消息!! 消息为空,错误是内部服务器错误!

  • 我习惯于使用Maven分发项目,通常,团队中的每个人(以及CI服务器)都可以依赖Maven进行打包、测试、运行等调试,但是,项目可以导入到Eclipse并通过主类启动(与Eclipse的Maven插件相反)。Spring-Boot就是一个很好的例子。导入这样的项目后,我可以只运行或调试主类。 然而,对于JavaFX11来说,编写一个配置所有必要依赖关系的pom.xml似乎很有效,但是当我将这样一个

  • 主要内容:创建临时配置,创建永久配置,创建新配置,共享配置IntelliJ IDEA 有很多方法可以为正在运行的项目创建配置。配置选项是: 创建临时配置 创建永久配置 在用户之间共享配置 创建临时配置 创建项目 创建Java 类 右键单击并选择运行选项 在运行菜单上添加了临时配置。 创建永久配置 当我们使用 Intellij Idea保存临时配置时,它可以转换为永久配置。要保存此配置,请单击“Run”菜单上的“Save Configuration”。我们

  • 您都知道将依赖项放入pom.xml文件并运行“MVN clean Install”的过程。当这个命令运行时,依赖项的jar文件被下载到。m2存储库中。 当我们使用IntelliJ和run/debug配置窗口运行应用程序时,IntelliJ如何知道在哪里查找依赖项的jar文件?IntelliJ中的每个GUI操作实际上都取代了命令行操作。当我们单击“运行”按钮时,在“幕后”提交的命令行操作是什么。我相

  • 我在tomcat上使用intellij运行项目,但是当运行tomcat时,错误会抛出 08,2015年4:00:37 org.apache.Catalina.core.containerbase startInternal:子容器在启动java.util.concurrent.executionexception:org.apache.Catalina.lifecycleexception:未能在

  • 在Java8中运行以下流示例: 产量: 当然,这并不奇怪。由于http://docs.oracle.com/javase/8/docs/api/index.html?overview-summary.html,流是顺序执行还是并行执行并不重要: 顺便说一下:使用(首选的)而不是生成相同的结果,用于顺序和并行执行。 JVM详细信息: