【问题标题】:Can't get images to show up, trying to display 5 random cards from directory file无法显示图像,尝试显示目录​​文件中的 5 张随机卡片
【发布时间】:2020-04-19 19:21:10
【问题描述】:

我不确定我做错了什么。我的窗格打开,但它是空白的。我将文件(卡)作为 src 文件夹中的目录(图像)。我试过用很多不同的方式写这个,但总是得到相同的结果。我只需要让图像真正显示出来。我不知道如何让卡片图像显示出来。非常感谢任何帮助。

package Assignment;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.Collections;

public class Assignment extends Application {
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage) {
        // Initialize card deck
        ArrayList<Integer> cards = getCards();

        // Create a FlowPane
        FlowPane pane = new FlowPane();
        pane.setVgap(10);
        pane.setHgap(10);
        pane.setPadding(new Insets(15, 15, 15, 15));

        for (int i = 0; i < 5; i++) {
            pane.getChildren().add(new ImageView(new Image("file:image/card/" + cards.get(i) + ".png")));
        }

        // Create scene and place it in the stage
        Scene scene = new Scene(pane,500, 300);
        primaryStage.setTitle("Poker 1");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private ArrayList<Integer> getCards() {
        ArrayList<Integer> cards = new ArrayList<>();
        for (int i = 0; i < 52; i++) {
            cards.add(i + 1);
        }

        Collections.shuffle(cards);
        return cards;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

【问题讨论】:

    标签: javafx


    【解决方案1】:

    你的卡片图片在哪个目录?

    单击运行按钮下拉菜单上的“编辑配置”,然后查看“工作目录”条目。前任。我的是 C:\Users\NAME\IdeaProjects\Test

    将卡片放在 C:\Users\NAME\IdeaProjects\Test\image\cards[1-52].png 中,使您的代码按编写方式为我工作。

    【讨论】:

    • 将图像从 src 文件夹中移到项目文件夹中。非常感谢!
    猜你喜欢
    • 2014-11-10
    • 2021-01-03
    • 2018-03-08
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多