我对 Java FX 非常陌生。我正在尝试使用场景生成器创建一个纸牌游戏应用程序以获取乐趣,但在更改 ImageView 组件的图像时遇到了问题。
src 文件夹包含两个包,一个包含所有图像,另一个包含所有代码。
在 Controller 类中,我尝试使用以下多种组合来更改图像,其中卡片是一个 ImageView
组件,它与 fx:id
匹配SceneBuilder
中 ImageView
的 code>:
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("文件:..images/cards/As.png"));
在每种情况下,我最终都会收到“无效 URL 或未找到资源”或“空指针”异常。
例如,如果我转到 .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 和宽度。
最佳答案
您的问题:
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);
}
关于Javafx 更改 ImageView 图像或图像 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51452785/