我的 JavaFX 应用程序需要能够找到 FXML 文件以加载它们FXMLLoader,以及样式表(CSS 文件)和图像。当我尝试加载这些时,我经常遇到错误,或者我尝试加载的项目在运行时根本没有加载。
对于 FXML 文件,我看到的错误消息包括
Caused by: java.lang.NullPointerException: location is not set
Run Code Online (Sandbox Code Playgroud)
对于图像,堆栈跟踪包括
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
Run Code Online (Sandbox Code Playgroud)
如何找出这些资源的正确资源路径?
我测试了这段代码,以便创建带图像的对话框.
final int xSize = 400;
final int ySize = 280;
final Color backgroundColor = Color.WHITE;
final String text = "SQL Browser";
final String version = "Product Version: 1.0";
final Stage aboutDialog = new Stage();
aboutDialog.initModality(Modality.WINDOW_MODAL);
Button closeButton = new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
aboutDialog.close();
}
});
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Image img = new Image("logo.png");
ImageView imgView = new ImageView(img);
grid.add(imgView, 0, 0);
grid.add(new …Run Code Online (Sandbox Code Playgroud) 我想这是一件非常简单的事情,但我无法支持它.我想要的只是在链接到fxml的ImageView上显示图像.这是我的代码:
package application;
import java.io.File;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
public class Main extends Application
{
@FXML
private ImageView imageView;
@Override
public void start(Stage primaryStage)
{
try
{
AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Hello World");
File file = new File("src/Box13.jpg");
Image image = new Image(file.toURI().toString());
imageView = new ImageView(image);
//root.getChildren().add(imageView);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) …Run Code Online (Sandbox Code Playgroud) 我ImageView在使用FXML 加载图像时遇到问题.
我的控制器类:
public class BoxViewController {
@FXML
private Label label_boxID;
@FXML
private ImageView boximage;
public void initData(ObservableList<BoxProperty> observableList,
BoxService sBox,
TableView tableview) {
this.label_boxID.setText(
String.valueOf(this.boxproperty.getPboxid()));
Image image = new Image("boximage.jpg");
this.boximage = new ImageView();
this.boximage.setImage(image);
}
}
Run Code Online (Sandbox Code Playgroud)
因此,使用文本设置标签有效,但图像不会出现在我的ImageView中.对于ImageView,我在FXML文件中添加了一个ID:
<ImageView fx:id="boximage"
disable="false"
fitHeight="150.0" fitWidth="200.0"
layoutX="69.0" layoutY="322.0"
pickOnBounds="true"
preserveRatio="true" />
Run Code Online (Sandbox Code Playgroud)
我很困惑,为什么这不起作用,因为标签工作,但图像不会加载.
我还检查了是否boximage为空,但事实并非如此.也没有例外.
我认为这将是一个简单的问题,但我找不到答案.我有一个与JavaFX Scene对象关联的ImageView对象,我想从磁盘加载大图像并使用ImageView依次显示它们.我一直在试图找到一种重复检查Image对象的好方法,当它在后台加载完成后,将它设置为ImageView,然后开始加载一个新的Image对象.我提出的代码(下面)有时会工作,有时则不会.我很确定我遇到了JavaFX和线程的问题.它有时加载第一张图像并停止.变量"processing"是类中的布尔实例变量.
在后台加载JavaFX中的图像并在加载后将其设置为ImageView的正确方法是什么?
public void start(Stage primaryStage) {
...
ImageView view = new ImageView();
((Group)scene.getRoot()).getChildren().add(view);
...
Thread th = new Thread(new Thread() {
public void run() {
while(true) {
if (!processing) {
processing = true;
String filename = files[count].toURI().toString();
Image image = new Image(filename,true);
image.progressProperty().addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number progress) {
if ((Double) progress == 1.0) {
if (! image.isError()) {
view.setImage(image);
}
count++;
if (count == files.length) {
count = 0; …Run Code Online (Sandbox Code Playgroud) 我想在对话框窗口中显示图像(保存在项目文件夹中),但是当我运行我的方法 showDialogWithImage 时,我得到 FileNotFoundExcpetion: imgs\pic1.jpg(系统找不到指定的文件),尽管图像位于那里。
我也尝试过以这种方式加载图像:
Image image = new Image(getClass().getResourceAsStream(path));,但遇到了同样的问题。
是否有其他一些可能性将图像加载到 ImageView ?
谢谢你的帮助!
我的 Java 代码位于项目文件夹中的 src\myProject\gui 中。
path="imgs\pic1.jpg" // imgs 位于项目文件夹中
public void showDialogWithImage(String path) {
final Stage dialogStage = new Stage();
logger.info(path);
InputStream is = null;
try {
is = new FileInputStream(path); // here I get FileNotFoundException
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(is);
ImageView view = new ImageView();
view.setImage(image);
Button btnOK = new Button("OK");
btnOK.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void …Run Code Online (Sandbox Code Playgroud)