如何使用JavaFX将图片添加至数组?运行不显示问题求助
It sounds like you're hitting a common set of pitfalls when working with JavaFX's ImageView and the scene graph. Let's break down the most likely reasons your images aren't showing up (even though similar logic worked with Node), and how to fix them:
1. You're not adding ImageViews to the scene graph
Even if you add ImageViews to your platFormList, they won't appear on screen unless you also add them to your Pane (the layout variable in your code). The list is just a collection for your own reference—JavaFX only renders nodes that are part of the scene hierarchy.
Fix: When creating each ImageView, add it to both the list and the layout:
ImageView platform = new ImageView(new Image("path/to/your/image.png")); platFormList.add(platform); layout.getChildren().add(platform); // This line is critical!
2. Image loading is failing silently
JavaFX's Image class doesn't throw exceptions if it can't find a file—it just fails quietly, leaving you with an empty ImageView. This is one of the most common gotchas.
Fix: Verify your image path is correct. If the image is in your project's resources folder, use this approach to load it properly:
// Correct way to load resources from the classpath Image platformImage = new Image(getClass().getResource("/images/platform.png").toExternalForm()); ImageView platform = new ImageView(platformImage);
You can also add a check to confirm the image loaded successfully:
if (platformImage.isError()) { System.err.println("Failed to load image: " + platformImage.getException()); }
3. Your AnimationTimer isn't starting or adding images correctly
If you're relying on the platFormTimer to add images dynamically, make sure you actually start the timer with platFormTimer.start(). Also, avoid adding duplicate ImageViews every frame—only create new ones when your game logic requires it.
Example of a proper timer setup:
platFormTimer = new AnimationTimer() { long lastSpawnTime = 0; @Override public void handle(long now) { // Spawn a new platform every 1 second if (now - lastSpawnTime >= 1_000_000_000) { lastSpawnTime = now; ImageView newPlatform = new ImageView(new Image(...)); newPlatform.setLayoutX(/* set horizontal position */); newPlatform.setLayoutY(/* set vertical position */); platFormList.add(newPlatform); layout.getChildren().add(newPlatform); // Add to scene graph } // Update platform positions if needed for (ImageView platform : platFormList) { platform.setLayoutY(platform.getLayoutY() + 1); } } }; platFormTimer.start(); // Don't forget this line!
4. ImageView is positioned off-screen or has no size
If your ImageView has fitWidth/fitHeight set to 0, or its layoutX/layoutY values place it outside your 800x500 scene bounds, it won't be visible.
Fix: Set explicit dimensions and position:
ImageView platform = new ImageView(image); platform.setFitWidth(100); // Set a visible width platform.setFitHeight(20); // Set a visible height platform.setLayoutX(400); // Center horizontally platform.setLayoutY(300); // Position vertically within the scene
Full Corrected Code Snippet
Here's a complete, minimal version of your code that addresses all these issues:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.Pane; import javafx.stage.Stage; import javafx.animation.AnimationTimer; import java.util.ArrayList; import java.util.List; public class PlatForm extends Application { List<ImageView> platFormList = new ArrayList<>(); private Pane layout; private Scene scene; private AnimationTimer platFormTimer; @Override public void start(Stage primaryStage) throws Exception { layout = new Pane(); scene = new Scene(layout, 800, 500); primaryStage.setScene(scene); primaryStage.setTitle("Platform Game"); primaryStage.show(); // Initialize and start the platform spawn timer platFormTimer = new AnimationTimer() { long lastSpawn = 0; @Override public void handle(long now) { // Spawn a new platform every 1 second if (now - lastSpawn >= 1_000_000_000) { lastSpawn = now; // Load image from resources (adjust path to match your project) Image platformImage = new Image(getClass().getResource("/platform.png").toExternalForm()); ImageView platform = new ImageView(platformImage); platform.setFitWidth(150); platform.setFitHeight(30); // Random horizontal position within scene bounds platform.setLayoutX((Math.random() * (scene.getWidth() - platform.getFitWidth()))); platform.setLayoutY(-platform.getFitHeight()); // Start off top of screen platFormList.add(platform); layout.getChildren().add(platform); // Add to scene graph to render } // Move platforms down and remove those that go off screen platFormList.removeIf(platform -> { platform.setLayoutY(platform.getLayoutY() + 2); return platform.getLayoutY() > scene.getHeight(); }); } }; platFormTimer.start(); } public static void main(String[] args) { launch(args); } }
Double-check your code against these points—chances are you missed one of these key steps (like adding to the layout or loading the image correctly). Let me know if you still run into issues!
内容的提问来源于stack exchange,提问作者Hussein Albirouti

