package tic.tac.toe.menu;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class TicTacToeMenu extends Application {
@Override
public void start(Stage primaryStage) {
Button start = new Button();
start.setText("How to Play?");
start.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("\n"+"The goal of tic-tac-toe is to get 3 of your pieces in a row vertically, horizontally, or diagonally ");
System.out.println("To play this game click inside a square to put down your piece, you choose to be 'x' or 'o' at the start");
}
});
StackPane root = new StackPane();
root.getChildren().add(start);
Scene scene = new Scene(root, 350, 250);
primaryStage.setTitle("Tic-Tac-Toe");
primaryStage.setScene(scene);
primaryStage.show();}
public void exit(Stage primaryStage) {
Button exit = new Button();
exit.setText("Quit?");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.exit(0);
})} // this line I get the error
StackPane root=new StackPane();
root.getChildren().add(exit);
Scene scene = new Scene(root, 350, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
我是Java的新手,我正在尝试制作一个菜单,但是在第40行我得到一个错误,说明非法启动类型,我很困惑这意味着什么。我认为我的语法错误但不确定要修复什么。
答案 0 :(得分:5)
从
更改代码})} // this line I get the error
到这个
}});
您需要连续2次关闭括号:第一次完成handle
方法,第二次完成EventHandler
外部近括号是完成setOnAction
调用。最后用一个半冒号来完成陈述。
答案 1 :(得分:5)
按如下方式更改exit.setOnAction
来电
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.exit(0);
}
}); // NOT })}
答案 2 :(得分:5)
将})}
替换为}});
。您必须按照打开它们的相反顺序关闭块。