代码之家  ›  专栏  ›  技术社区  ›  aman

如何从Java FX中当前类的启动函数打开另一个类的启动函数?

  •  0
  • aman  · 技术社区  · 6 年前

    我想做一个数独游戏 JAVA FX 。我必须打开一个包含两个按钮的欢迎页面 NEW GAME CONTINUE GAME 。如果 新游戏 点击它应该会打开 Sudoku.java 应该打开舞台,关闭 Welcome.java 文件和舞台。我怎么能做到这一点,甚至有可能做到这一点?

    爪哇 :

    package application;
    
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.*;
    
    public class Welcome extends Application{
        @Override
        public void start(Stage primaryStage) {
            AnchorPane root = new AnchorPane();
            root.setPrefHeight(400);
            root.setPrefWidth(350);
            try {
                Scene scene = new Scene(root, 350, 400);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
    
            Label label = new Label("Welcome to sudoku puzzle world!");
            label.setLayoutX(5);
            label.setLayoutY(50);
            label.setPrefHeight(60);
            label.setPrefWidth(330);
            root.getChildren().add(label);
    
            Button new_game = new Button("NEW GAME");
            new_game.setLayoutX(100);
            new_game.setLayoutY(140);
            new_game.setPrefHeight(60);
            new_game.setPrefWidth(140);
            new_game.setOnMouseClicked(new EventHandler<MouseEvent>()
            {
                public void handle(MouseEvent t) {
                     // HOW TO OPEN THE Sudoku.java INSIDE HERE
                }
            });
            root.getChildren().add(new_game);
    
            Button continue_game = new Button("CONTINUE GAME");
            continue_game.setLayoutX(100);
            continue_game.setLayoutY(220);
            continue_game.setPrefHeight(60);
            continue_game.setPrefWidth(140);
            root.getChildren().add(continue_game);
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    爪哇语 :

    package application;
    
    import java.util.*;
    import javafx.scene.shape.Rectangle;
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.*;
    import javafx.scene.paint.Color;
    
    public class Sudoku extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            AnchorPane root = new AnchorPane();
            root.setPrefHeight(580);
            root.setPrefWidth(450);
            try {
                Scene scene = new Scene(root, 450, 580);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
            System.out.println("OPENED");
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    
    0 回复  |  直到 6 年前