首页 文章

如何在javafx中从另一个窗口显示图像?

提问于
浏览
0

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

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();
    }
}

1 回答

  • 2

    您遇到异常,因为您在其中添加了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();
        }
    }
    

相关问题