为什么JavaFX在IDE项目运行中显示图标图像,但在项目构建中不显示图像?

问题描述 投票:0回答:1

JavaFX 在 IDE 项目运行中显示图标图像,但在项目构建中不显示图像,奇怪的是,当您单击那里的空间时,按钮应该仍然执行其功能。 为了具体说明问题,我将显示屏幕截图 - 两个有问题的图像是我的占位符:

构建过程中唯一的两个警告是:

Missing POM for unknown.binary:AbsoluteLayout:jar:SNAPSHOT
Missing POM for unknown.binary:AbsoluteLayout:jar:SNAPSHOT

该项目是我继承的遗留代码,因此我可能不知道这里有一些明显的解决方案。

奇怪的是,如果我从编译版本的文件夹中删除图像(不在代码存储库中),图像仍然存在。这让我相信它们可能存储在 jar build 本身中?我有点预计如果没有它,罐子就不会启动,或者图标图像会消失。

我还尝试更改 CSS 文件中资源的路径,例如添加“../”,但这没有任何改变。

gameScreen.fxml:

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

<?scenebuilder-stylesheet vista.css?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<AnchorPane id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" stylesheets="@../styles/common.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="org.balancer.controllers.GameScreenController">
   <children>
      <Button id="goBackButton" layoutX="1100.0" layoutY="34.0" mnemonicParsing="false" onAction="#handleHomeButtonPressed" prefHeight="100.0" prefWidth="100.0" stylesheets="@../styles/common.css" />
      <Label id="screenNameLabel" layoutX="49.0" layoutY="34.0" prefHeight="58.0" prefWidth="457.0" stylesheets="@../styles/common.css" text="Wybierz grę" />
      <HBox id="gamesHBox" alignment="CENTER" layoutX="60.0" layoutY="134.0" prefHeight="288.0" prefWidth="1132.0" spacing="25.0">
         <children>
            <Button id="skiChallengeButton" mnemonicParsing="false" onAction="#skiChallengeButtonHandling" prefHeight="152.0" prefWidth="149.0" stylesheets="@../styles/gameScreen.css" translateX="-80.0" />
            <Button id="jLedButton" mnemonicParsing="false" onAction="#janekLEDButtonHandling" prefHeight="149.0" prefWidth="149.0" stylesheets="@../styles/gameScreen.css" translateX="-80.0" />
            <Button id="archeryButton" mnemonicParsing="false" onAction="#archeryButtonHandling" prefHeight="150.0" prefWidth="150.0" stylesheets="@../styles/gameScreen.css" translateX="-80.0" />
            <Button id="jScoreButton" mnemonicParsing="false" onAction="#janekScoreButtonHandling" prefHeight="150.0" prefWidth="150.0" stylesheets="@../styles/gameScreen.css" translateX="-80.0" />
            <Button id="stuntGPButton" layoutX="693.0" layoutY="79.0" mnemonicParsing="false" onAction="#stuntGPButtonHandling" prefHeight="150.0" prefWidth="150.0" stylesheets="@../styles/gameScreen.css" translateX="-80.0" />
            <Button id="NewGameButton" mnemonicParsing="false" onAction="#NewGameHandling" prefHeight="150.0" prefWidth="150.0" stylesheets="@../styles/gameScreen.css" />
         </children>
      </HBox>
      <HBox id="gamesHBox2" alignment="CENTER" layoutX="106.0" layoutY="415.0" prefHeight="288.0" prefWidth="1068.0" spacing="70.0">
         <children>
            <Button id="NewGameButton" mnemonicParsing="false" onAction="#NewGameHandling" prefHeight="152.0" prefWidth="149.0" stylesheets="@../styles/gameScreen.css" />
         </children>
      </HBox>
   </children>
</AnchorPane>

游戏屏幕.css

#skiChallengeButton {
    -fx-background-image: url("/images/gameIcons/skichallenge2015Logo.png");
    -fx-background-position: center center;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-background-size: cover;
    -fx-border-width: 0;
}
#skiChallengeButton:hover  {
    -fx-background-image: url("/images/gameIcons/skichallenge2015LogoFocused.png");
}

#NewGameButton {
    -fx-background-image: url("/images/gameIcons/New_Game.png");
    -fx-background-position: center center;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-background-size: cover;
    -fx-border-width: 0;
}

#NewGameButton:hover {
    -fx-background-image: url("/images/gameIcons/New_Game_Focused.png");
 }

#archeryButton {
    -fx-background-image: url("/images/gameIcons/archeryLogo.png");
    -fx-background-position: center center;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-background-size: cover;
    -fx-border-width: 0;
}
#archeryButton:hover  {
    -fx-background-image: url("/images/gameIcons/archeryLogoFocused.png");
}
#jScoreButton {
    -fx-background-image: url("/images/gameIcons/jScoreLogo.png");
    -fx-background-position: center center;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-background-size: cover;
    -fx-border-width: 0;
}
#jScoreButton:hover  {
    -fx-background-image: url("/images/gameIcons/jScoreLogoFocused.png");
}
#jLedButton {
    -fx-background-image: url("/images/gameIcons/jLedLogo.png");
    -fx-background-position: center center;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-background-size: cover;
    -fx-border-width: 0;
}
#jLedButton:hover  {
    -fx-background-image: url("/images/gameIcons/jLedLogoFocused.png");
}
#stuntGPButton {
    -fx-background-image: url("/images/gameIcons/stuntGPLogo.png");
    -fx-background-position: center center;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-background-size: cover;
    -fx-border-width: 0;
}
#stuntGPButton:hover  {
    -fx-background-image: url("/images/gameIcons/stuntGPLogoFocused.png");
}

GameScreenController.java:

import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.balancer.db.tables.User;
import org.balancer.interpreter.*;
import java.util.Optional;
import org.balancer.PropertiesHelper;

public class GameScreenController implements Initializable, ScreenController {

    @FXML
    Button gameButton;
    Interpreter currentInterpreter;
    ScreenNavigator myNavigator;
    private ResourceBundle strings;
    private static final Logger logger = LogManager.getLogger(GameScreenController.class);

    @Override
    public void setScreenParent(ScreenNavigator screenParent) {
        myNavigator = screenParent;
    }

    public void changeButtonText(String value) {

        gameButton.setText(value);
    }

    @FXML
    private void handleHomeButtonPressed(ActionEvent event) {
        myNavigator.setScreen(ScreenNavigator.homeScreen);
    }

    @FXML
    private void janekLEDButtonHandling(ActionEvent event) {
        showViewerDialogBox();
        currentInterpreter = new JLedInterpreter();
        showInterpreterDialogBox();
    }

    @FXML
    private void janekScoreButtonHandling(ActionEvent event) {
         showViewerDialogBox();
        currentInterpreter = new JScoreInterpreter();
        showInterpreterDialogBox();
    }

    @FXML
    private void archeryButtonHandling(ActionEvent event) {
         showViewerDialogBox();
        currentInterpreter = new ArcheryInterpreter();
        showInterpreterDialogBox();
    }

    @FXML
    private void skiChallengeButtonHandling(ActionEvent event) {
        currentInterpreter = new SkiChallengeInterpreter();
        showInterpreterDialogBox();
    }

    @FXML
    private void stuntGPButtonHandling(ActionEvent event) {
        currentInterpreter = new StuntGPInterpreter();
        showInterpreterDialogBox();
    }
    
    @FXML
    private void NewGameHandling(ActionEvent event) {
        //currentInterpreter = new NewGameKeyboardInterpreter();
        currentInterpreter = new NewGameInterpreter();
        showInterpreterDialogBox();
    }
    


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Locale locale = User.currentUser.getLocale();
        String bundleName = "stringsBundle." + this.getClass().getSimpleName();
        strings = ResourceBundle.getBundle(bundleName, locale);

控制器文件管理按钮的功能,根本不重要。 你在代码中看到什么了吗?我一开始没有包含它,因为它似乎是一个明显的问题,特别是我从字面上复制了以前的“NewGameButton”,并且它显然可以在 IDE 中工作。这意味着代码至少在一个地方按预期工作。

编辑 01.03.24

当我说项目构建时,我的意思只是一个 Jar 可执行文件,我将其放置在前人设计的指定文件夹中。我将其称为“应用程序环境”的指定文件夹具有以下结构: File structure File Structure 2

当我测试它是否首先取决于所述图像的位置或存在时,我已将图像文件夹放在多个位置。看来不是。 构建命令如下:

cd D:\projekty\mag\JanekStepInterpreter-master\BalancerInterpreter; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_211" cmd /c "\"D:\\JDK\\JDK_proper\\NetBeans-17\\netbeans\\java\\maven\\bin\\mvn.cmd\" -Dmaven.ext.class.path=D:\\JDK\\JDK_proper\\NetBeans-17\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar -Dfile.encoding=UTF-8 --no-transfer-progress clean install" 

看起来是Maven项目。 波姆 :

<?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>org.balancer</groupId>
    <artifactId>BalancerInterpreter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.json</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-configuration2</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.196</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.4.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.rxtx</groupId>
            <artifactId>rxtx</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>unknown.binary</groupId>
            <artifactId>AbsoluteLayout</artifactId>
            <version>SNAPSHOT</version>
        </dependency>
       
        <dependency>
            <groupId>org.glassfish.tyrus.bundles</groupId>
            <artifactId>tyrus-standalone-client-jdk</artifactId>
            <version>1.13</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.1.Final</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.balancer.FxMenu</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
    <name>BalancerInterpreter</name>
</project>

在“target”文件夹中构建的项目中有: project tree

当您在目标文件夹中启动 jar 文件时,它不起作用。它需要在

Balancer 3.0.0.0.
文件夹(在“应用程序环境”内)中运行 - 这就是这个项目自从我得到它以来的运行方式。我确信它需要
rxtx-2.1.7
和应用程序文件夹才能工作(
Archer
JanekLED
等) 它还需要一个 glassfish 服务器文件夹,这是一个单独的项目。

当涉及到 jar 文件中“images”文件夹的位置时,它位于 jar 的顶层:jar file uzipped 这似乎是正确的,当查看内部时,它还包含

New_Game.png
,这是有问题的图标。

我确实尝试理解并应用[Eden JavaFx资源指南]中所说的内容,但是即使我像这样用

../
回溯:
url(../abc/cde.png)
效果仍然相同。

关于图标ID:上面的图像是最近添加的,以测试它是否会出现在上面的HBox中,因为当我得到它时,这个框已经在代码中了。我想也许我新制作的 HBox 不知怎么坏了。但无论只有一个具有唯一ID的图标还是有两个这样的图标,效果都是一样的。不过还是谢谢您 - 我会继续使用一个 ID 或制作另一个 ID。

我看不到任何

module-info.java
。既不通过查看我的项目树,也不通过使用文件资源管理器中的搜索选项

javafx icons legacy-code
1个回答
0
投票

事实证明,当我用油漆制作一个全新的图标时,一切正常。 (而不是在画图中编辑现有图标并更改名称)

遗憾的是,我不知道是什么让程序决定不显示问题中显示的原始图像,但图像文件本身似乎有问题。

© www.soinside.com 2019 - 2024. All rights reserved.