这是我的代码:
Controller .java
package sample
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class Controller {
@FXML
Button button;
@FXML
TextField text;
@FXML
ImageView iv;
public void handleBtn()
{
String name = text.getText();
String address = "./res/"+name+".png";
Image image = new Image(address);
iv.setImage(image);
}
}
Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
和示例.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="387.0" prefWidth="481.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.171" fx:controller="sample.Controller">
<children>
<TextField fx:id="text" layoutX="41.0" layoutY="64.0" />
<Button fx:id="button" layoutX="64.0" layoutY="169.0" mnemonicParsing="false" onAction="#handleBtn" text="Button" />
<ImageView fx:id="iv" fitHeight="261.0" fitWidth="209.0" layoutX="222.0" layoutY="56.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
当我运行我的项目并在文本字段中输入 1 并单击按钮时,它会显示图片。现在,在不关闭程序的情况下,我将一个 2.png 文件添加到 res 文件夹中。然后在文本字段中输入 2。单击按钮后,它给我“java.lang.IllegalArgumentException:无效的URL:无效的URL或找不到资源”并且无法加载图像。但如果我终止程序并重新运行它,输入 2 并单击按钮就可以正常工作。
最佳答案
这对我有用。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class FxTemp extends Application {
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Image image = new Image("file:///home/username/Desktop/test.png");
primaryStage.setTitle("animated");
ImageView imageView = new ImageView(image);
imageView.addEventHandler(MouseEvent.MOUSE_CLICKED, evt->{
System.out.println("click");
Image imager = new Image("file:///home/username/Desktop/test.png");
imageView.setImage(imager);
});
StackPane root = new StackPane();
root.getChildren().add(imageView);
primaryStage.setScene(new Scene(root, 512, 512));
primaryStage.show();
}
}
如果我更改 test.png 并单击 View ,我会看到新图像。
您实际上并不应该将文件添加到您的“资源”中,这些文件是您在执行之前提供的文件。 Intellij 会将文件复制到某个位置并在类路径上使用该位置。然后您可以通过 getResource
访问它们。
如果您要在一个文件夹中添加文件,然后您想要访问这些文件,您可以执行类似的操作。
String nextImage = new File("./res/image.png").toURI().toString();
然后,如果您的路径正确,您应该能够加载新图像。
关于Javafx无法加载新添加的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56703254/