用按钮打开java文件(javafx)

时间:2018-05-02 17:04:29

标签: java javafx

我有一个用javafx制作的应用程序和另一个带有这个项目菜单的类。在这个菜单中,我有两个按钮,一个工作(退出buuton),我想要buttonStart打开我的Main类。如何推出它?

Button buttonStart = new Button("START GAME");

Button buttonExit = new Button("EXIT");

    buttonExit.setOnMouseClicked(event -> System.exit(0));

我的菜单:

package pl.main;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Menu extends Application {

    private BorderPane layout;
    private Scene scene;

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

    @Override
    public void start(Stage window) throws Exception {
        layout = new BorderPane();
        scene = new Scene(layout, 720, 480);

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(10);
        hbox.setStyle("-fx-background-color: #F9A825;");

        Button buttonStart = new Button("START GAME");
        buttonStart.setPrefSize(100, 20);
        buttonStart.setStyle("-fx-background-color: #E65100;");

        Button buttonExit = new Button("EXIT");
        buttonExit.setPrefSize(100, 20);
        buttonExit.setStyle("-fx-background-color: #E65100;");

        buttonExit.setOnMouseClicked(event -> System.exit(0));
        hbox.getChildren().addAll(buttonStart, buttonExit);

        layout.setCenter(hbox);

        window.setScene(scene);
        window.show();
    }
}

我想推出的课程:

package pl.main;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;

public class Main extends Application {

    private HashMap<KeyCode, Boolean> keys = new HashMap<KeyCode, Boolean>();
    private ArrayList<Node> blocks = new ArrayList<Node>();
    private Pane appRoot = new Pane();

    private Pane gameRoot = new Pane();
    private Pane uiRoot = new Pane();

    private Node player;
    private Point2D playerGoDown = new Point2D(0, 0);
    private Point2D playerGoRight = new Point2D(0, 0);
    private boolean canJump = true;
    private int levelWidth;

    private void initContent() {
        Rectangle background = new Rectangle(720, 480);
        // BackgroundFill(Color.WHITE);

        levelWidth = LevelData.LEVEL1[0].length();

        for (int i = 0; i < LevelData.LEVEL1.length; i++) {
            String map = LevelData.LEVEL1[i] + LevelData.LEVEL2[i]+ LevelData.LEVEL1[i]+ LevelData.LEVEL2[i]+ LevelData.LEVEL1[i]+ LevelData.LEVEL2[i];

            String line = map;
            for (int j = 0; j < line.length(); j++) {
                switch (line.charAt(j)) {
                case '0':
                    break;
                case '1':
                    Node block = createEntity(j * 30, i * 30, 30, 30, Color.ORANGE);
                    blocks.add(block);
                    break;
                }
            }
        }
        player = createEntity(0, 350, 40, 40, Color.YELLOW);
        // a ????????????
        player.translateXProperty().addListener((a, old, newValue) -> {
            int offset = newValue.intValue();
            //if (offset > 360 && offset < levelWidth - 360) {
                gameRoot.setLayoutX(-(offset - 360));
            //}
        });
        appRoot.getChildren().addAll(background, gameRoot, uiRoot);
    }

    private void update() {
        if (isPressed(KeyCode.W) && player.getTranslateY() >= 0) {
            jumpPlayer();
        }
        if (playerGoDown.getY() < 10) {
            playerGoDown = playerGoDown.add(0, 1);
        }
        movePlayerY((int) playerGoDown.getY());

        if (player.getTranslateX() <= levelWidth - 5) {
            // movePlayerX(5);
            movePlayerRight();
        }

        if (playerGoRight.getX() < 0) {
            playerGoRight = playerGoRight.add(0, 1);
        }
        movePlayerX((int) playerGoRight.getX());
    }

    private void movePlayerX(int value) {
        boolean movingRight = value > 0;
        for (int i = 0; i < Math.abs(value); i++) {
            for (Node block : blocks) {
                if (player.getBoundsInParent().intersects(block.getBoundsInParent())) {
                    if (movingRight) {
                        if (player.getTranslateX() + 40 == block.getTranslateX()) {
                            return;
                        }
                    } else {
                        if (player.getTranslateX() == block.getTranslateX() + 60) {
                            return;
                        }
                    }
                }
            }
            player.setTranslateX(player.getTranslateX() + (movingRight ? 1 : -1));
        }
    }

    private void movePlayerY(int value) {
        boolean movingDown = value > 0;
        for (int i = 0; i < Math.abs(value); i++) {
            for (Node block : blocks) {
                if (player.getBoundsInParent().intersects(block.getBoundsInParent())) {
                    if (movingDown) {
                        if (player.getTranslateY() + 40 == block.getTranslateY()) {
                            canJump = true;
                            return;
                        }
                    } else {
                        if (player.getTranslateY() == block.getTranslateY() + 60) {
                            return;
                        }
                    }
                }
            }
            player.setTranslateY(player.getTranslateY() + (movingDown ? 1 : -1));
        }
    }

    private void jumpPlayer() {
        if (canJump) {
            playerGoDown = playerGoDown.add(0, -10);
            canJump = false;
        }
    }

    private void movePlayerRight() {
        playerGoRight = playerGoRight.add(10, 0);
    }

    private Node createEntity(int x, int y, int w, int h, Color color) {
        Rectangle entity = new Rectangle(w, h);
        entity.setTranslateX(x);
        entity.setTranslateY(y);
        entity.setFill(color);
        gameRoot.getChildren().add(entity);
        return entity;

    }

    private boolean isPressed(KeyCode key) {
        return keys.getOrDefault(key, false);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        initContent();
        Scene scene = new Scene(appRoot);
        scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
        scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
        primaryStage.setTitle("Jetpack gameplay");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setResizable(false);
        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                update();
            }
        };
        timer.start();
    }

    public static void main(String[] args) {

        launch(args);
    }
}

将来我想把当前的Main.java改成普通的类,因为现在我有两个独立工作的类。

1 个答案:

答案 0 :(得分:0)

如果你想进入另一个页面,你必须加载游戏场景。

查看JavaDoc中的Scene类。

一旦你有了场景,你只需要在你的按钮上添加一个ActionEvent来显示你的页面。