【问题标题】:How can I get an image to show from another window in javafx?如何从 javafx 中的另一个窗口显示图像?
【发布时间】:2017-11-05 08:10:55
【问题描述】:

我想允许用户使用按钮进入下一个窗口。然后此按钮显示图像。但是,当我尝试运行该程序时,会抛出异常。这是我的代码:

package matchingcards;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MatchingCards extends Application {

    Stage window;
    Scene start, game;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;

        // When user clicks start, program enters game
        Button btn = new Button("Start Game");
        btn.setOnAction(e -> window.setScene(game));

        // Create the display for start button
        GridPane pane1 = new GridPane();
        pane1.getChildren().addAll(pane1, btn);
        start = new Scene(pane1, 200, 200);

        // Create display for game
        GridPane pane2 = new GridPane();
        Image back = new
                Image("https://i.pinimg.com/736x/c1/59/b4/" +
                      "c159b4738dae9c9d8d6417228024de8d--" +
                      "playing-card-design-card-card.jpg", 300, 200, false, false);
        pane2.getChildren().addAll(new ImageView(back));
        Scene game = new Scene(pane2, 500, 500);

        window.setScene(start);
        window.setTitle("Matching Cards");
        window.show();
    }
}

【问题讨论】:

  • 我编辑了你的帖子,你也需要在问题中包含异常

标签: java javafx imageview


【解决方案1】:

您遇到了异常,因为您在其内部添加了 pane1

GridPane pane1 = new GridPane();
pane1.getChildren().addAll(pane1, btn);

我已经稍微更正了你的代码:

package matchingcards;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class MatchingCards extends Application {

    Stage window;
    Scene start, game;

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        window = primaryStage;

        // When user clicks start, program enters game
        Button btn = new Button("Start Game");

        btn.setOnAction(e -> window.setScene(game));

        // Create the display for start button
        GridPane pane1 = new GridPane();
        pane1.getChildren().addAll(btn);
        start = new Scene(pane1, 200, 200);

        // Create display for game
        GridPane pane2 = new GridPane();
        Image back = new
                Image(
                "https://i.pinimg.com/736x/c1/59/b4/c159b4738dae9c9d8d6417228024de8d--" +
                "playing - card - design - card - card.jpg",
                300, 200, false, false);
        pane2.getChildren().addAll(new ImageView(back));
        game = new Scene(pane2, 500, 500);

        window.setScene(start);
        window.setTitle("Matching Cards");
        window.show();
    }
}

【讨论】:

    猜你喜欢
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2019-05-29
    • 2021-05-09
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    相关资源
    最近更新 更多