Java FX 更改 ImageView 图像或图像 URL?

Java FX Changing ImageView image or image URL?

我对 Java FX 很陌生。我正在尝试使用场景生成器创建纸牌游戏应用程序以获得乐趣,但在更改 ImageView 组件的图像时遇到了问题。

src 文件夹包含两个包,一个用于所有图像,另一个包含所有代码。

在控制器 class 中,我尝试使用以下多种组合来更改图像,其中卡片是 ImageView 组件,与 fx:id 匹配SceneBuilder 中的 ImageView

card.setImage(new Image("../images/cards/As.png");

card.setImage(new Image("@../images/cards/As.png");

card.setImage(new Image(getClass().getResource("As.png").toExternalForm()));

card.setImage(new Image("File:..images/cards/As.png"));

在每种情况下,我最终都会得到 "Invalid URL or Resource not found" 或 "Null Pointer" 异常。

例如,如果我转到 .fxml 并将 URL 编辑为“@../images/cards/As.png”,那么它可以工作,但是在使用 setImage 方法时我不能做吧。

我从这里的其他线程那里得到了这些想法,他们似乎在为其他人工作。我的图片放错地方了吗?无法更改 .fxml 文件中某些内容的图像 URL 吗?如果有人能帮我解决这个问题,我将不胜感激。

项目结构:


我开始了一个名为 JavaFXApplication1 的新项目来测试图像更改功能。我正在尝试通过单击按钮来执行此操作。 有两个源码包,从上面的项目结构可以看出。 'images' 包只包含我想在我的应用程序中使用的所有 .png,然后另一个包称为 'javafxapplication1',包含 3 个文件。以下是每个代码:

FXMLDocument.fxml

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

<?import javafx.geometry.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication1.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <ImageView id="rat" fx:id="card" cache="true" fitHeight="105.0" fitWidth="118.0" layoutX="39.0" layoutY="24.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="39.0" AnchorPane.rightAnchor="208.68595123291016">
         <image>
            <Image url="@../images/cards/back.png" />
         </image></ImageView>
    </children>
</AnchorPane>

FXMLDocumentController.java

package javafxapplication1;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

/**
 *
 * @author wesley
 */
public class FXMLDocumentController implements Initializable {
    
    @FXML
    private Label label;
    @FXML
    private ImageView card;
 
    @FXML
    private Button button;
    
    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
        
        System.out.println(getClass().getResourceAsStream("/images/cards/As.png"));
        Image image = new Image(getClass().getResourceAsStream("/images/cards/As.png"));
        card.setFitHeight(726); //726
        card.setFitWidth(500); //500
        card.setImage(image);
        System.out.println("You changed the pic ");
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        //File file = new File("/images/cards/Ad.png");
        //Image image = new Image(file.toURI().toString());
        card = new ImageView("images/cards/Ad.png");
        //card.setFitHeight(726); //726
       // card.setFitWidth(500); //500
    }    
 
}

JavaFXApplication1.java

package javafxapplication1;

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

/**
 *
 * @author 
 */
public class JavaFXApplication1 extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        
        Scene scene = new Scene(root);
        
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

当我 运行 应用程序时,它加载正常,其中包含在场景生成器中定义的图像(注意:我使用文档相对路径,不确定这是否理想或可能是一个问题).单击按钮后,将执行 setImage 调用,但我看不到图像有任何变化。我知道它会执行,因为这是我的输出:

You clicked me! sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@1db87651 You changed the pic

此外,我不确定是否应该从初始化方法中设置 fitHeight 和 width。

您的问题:

card = new ImageView("images/cards/Ad.png");

这是无效代码。您在 FXML 中创建了卡片。当您说 = new ImageView(...); 时,您实际上不再引用 FXML 中创建的 Card/ImageView

如果您需要在初始化方法中初始化 Card/ImageView,请执行以下操作:

@Override
public void initialize(URL url, ResourceBundle rb)
{
    Image image = new Image(getClass().getResourceAsStream("/images/cards/Ad.png"));
    card.setFitHeight(100); //726
    card.setFitWidth(200); //500
    card.setImage(image);
}

有两种加载图片的方法(我先说)
当你想将图片文件夹嵌入jar(包)中时,使用Sedrick提到的方法文件, 但是如果你想让 jar 更轻,并且将图像文件夹放在 jar 之外,我宁愿使用这种方式

card.setImage(new Image("file:images/cards/As.png"));

在我们的案例中,文件关键字使文件浏览器从项目文件夹开始查找 JavaFXApplication1

我能做到的最一致的方法 - 使用 Netbeans 作为我的 IDE - 是

new Image(JavaFXApplication1.class.getClass().getResource("/img/card.jpg").toString());

而我的项目目录源码结构中有img目录与包同级。在文件系统中,.java 文件是 ../img/card.jpg,但使用 GetResource 将其指向相当于您的 .../JavaFXApplication1/src/ 目录