[英]Java FX Changing ImageView image or image URL?
我是Java FX的新手。 我正在尝试使用场景生成器创建一个纸牌游戏应用程序,但很有趣,但是更改ImageView组件的图像时遇到了问题。
src文件夹包含两个软件包,一个包含所有图像,另一个包含所有代码。
在控制器类中,我尝试使用以下多种组合来更改图像,其中卡是ImageView
组件,与SceneBuilder
ImageView
的fx:id
相匹配:
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"));
在每种情况下,我最终都会收到“无效的URL或找不到资源”或“空指针”异常。
例如,如果我转到.fxml并将URL编辑为“ @ .. / images / cards / As.png”,则它可以工作,但是当使用setImage方法时,我做不到。
我从其他线程那里得到的想法似乎是在为其他人工作的。 我的图像放置在错误的位置吗? 无法更改.fxml文件中某些内容的图像URL? 如果有人可以帮助我,我将非常感激。
项目结构:
我开始了一个名为JavaFXApplication1的新项目,以测试图像更改功能。 我试图通过单击按钮来做到这一点。 从上面的项目结构中可以看到,有两个源程序包。 有一个“图像”包,其中仅包含我要在我的应用程序中使用的所有.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调用,但是我看不到图像中的任何更改。 我知道它会执行,因为这是我的输出:
你点击了我! sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@1db87651您更改了图片
另外,我不确定是否应该通过initialize方法设置fitHeight和width。
你的问题:
card = new ImageView("images/cards/Ad.png");
这是无效的代码。 您在FXML
创建了卡。 当你说= new ImageView(...);
,您实际上不再引用FXML
创建的Card/ImageView
。
如果您需要在initialize方法中初始化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);
}
加载图像有两种方法 (预检)
当您要将images文件夹嵌入jar(打包)文件时,可以使用Sedrick提到的方法, 但是,如果您想使Jar轻巧,将images文件夹保留在jar之外,我宁愿使用这种方式
card.setImage(new Image("file:images/cards/As.png"));
其中,file关键字使文件浏览器从我们的案例JavaFXApplication1中的项目文件夹开始查找
我能做到的最一致的方法-使用Netbeans作为我的IDE-是
new Image(JavaFXApplication1.class.getClass().getResource("/img/card.jpg").toString());
我的项目目录源结构中的img
目录与软件包相同。 在文件系统中,从.java文件为../img/card.jpg
,但使用GetResource
会将其指向.../JavaFXApplication1/src/
目录的等效目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.