public class MainMenu extends SnakeAndLadder {
private Group tileGroup = new Group();
public boolean started=false;
private Parent createConetent(){
StackPane root = new StackPane();
root.setPrefSize(width* tileSize,(height* tileSize)+80);
root.getChildren().addAll(tileGroup);
Button start = new Button("Start");
start.setTranslateX(10);
start.setTranslateY(-500);
start.setPrefSize(300,150);
start.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
gameStarted();
}
});
Button exit = new Button("Exit");
exit.setTranslateX(10);
exit.setTranslateY(-300);
exit.setPrefSize(300,150);
exit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
tileGroup.getChildren().addAll(start,exit);
return root;
}
@Override
public void start(Stage primaryStage)throws Exception{
Scene scene = new Scene(createConetent());
primaryStage.setTitle("Snake And Ladder");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String args[]){
launch(args);
}
}
我使用extend来获得与主游戏相同的GUI大小。但是,当我单击开始按钮时,出现错误。
我对此并不陌生,我不确定它是否可以那样工作,所以我想请您提出建议。任何帮助将不胜感激。
这是游戏的代码:
public class SnakeAndLadder extends Application {
public static final int tileSize = 80;
public static final int width = 10;
public static final int height = 10;
public Group titleGroup = new Group();
private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(width * tileSize, (height * tileSize) + 80);
root.getChildren().addAll(titleGroup);
for(int i=0; i<width; i++){
for(int j=0; j<height; j++){
Tile tile = new Tile(tileSize, tileSize);
tile.setTranslateX(j * tileSize);
tile.setTranslateY(i * tileSize);
titleGroup.getChildren().add(tile);
}
}
return root;
}
@Override
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setTitle("Quiz Snake and Ladder Game");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
public void gameStarted() {
launch();
}
}
答案 0 :(得分:0)
package sample;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
import javax.swing.*;
public class SnakeAndLadder extends Application {
public static final int tileSize = 80;
public static final int width = 10;
public static final int height = 10;
public Group titleGroup = new Group();
private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(width * tileSize, (height * tileSize) + 80);
root.getChildren().addAll(titleGroup);
for(int i=0; i<(width-.14); i++){
for(int j=0; j<height; j++){
Tile tile = new Tile(tileSize, tileSize);
tile.setTranslateX(j * tileSize);
tile.setTranslateY(i * tileSize);
titleGroup.getChildren().add(tile);
}
}
return root;
}
//@Override
public void start(Stage primaryStage)throws Exception {
Scene scene1 = new Scene(createConetent(primaryStage));
primaryStage.setTitle("Main Menu");
primaryStage.setScene(scene1);
primaryStage.show();
}
private Parent createConetent(Stage primaryStage)throws Exception{
Pane root = new Pane();
root.setPrefSize(width* tileSize,height* tileSize);
root.getChildren().addAll(titleGroup);
Button start = new Button("Start");
start.setTranslateX(253);
start.setTranslateY(100);
start.setPrefSize(300,150);
start.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
gameStarted=true;
Scene scene = new Scene(createContent());
primaryStage.setTitle("Quiz Snake and Ladder Game");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
});
Button exit = new Button("Exit");
exit.setTranslateX(253);
exit.setTranslateY(540);
exit.setPrefSize(300,150);
exit.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
Image img = new Image("MenuBG.jpg");
ImageView imageView = new ImageView();
imageView.setImage(img);
imageView.setFitWidth(800);
imageView.setFitHeight(800);
Button how = new Button("How to play");
how.setTranslateX(253);
how.setTranslateY(320);
how.setPrefSize(300,150);
how.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
}
});
titleGroup.getChildren().addAll(imageView,start,exit,how);
return root;
}
public static void main(String args[]){
launch(args);
}
}