【问题标题】:Java Illegal start type ) expected [closed]Java非法启动类型)预期[关闭]
【发布时间】:2014-01-18 15:38:57
【问题描述】:
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 行我收到一个错误,提示类型非法开始,我很困惑这意味着什么。我认为我的语法错误,但不确定要修复什么。

【问题讨论】:

  • 老实说,SO 不是语法检查器。并使用一个 IDE,它会告诉你缺少括号/括号/等。
  • 这是公共无效出口下的一个,我用评论标记了它
  • @BrianRoach 确定是的……看看下面的答案。效率也很高——众包是吗?
  • @bblincoe 我知道 ::sigh::

标签: java syntax menu compiler-errors javafx


【解决方案1】:

修改代码

})} // this line I get the error

到这里

}}); 

你需要两个连续的右大括号:第一个完成handle方法,第二个完成EventHandler

外圆括号结束setOnAction 调用。最后是一个分号来完成语句。

【讨论】:

  • 我什至建议“下一行”最后三个字符以进行适当的缩进。
【解决方案2】:

如下更改您的exit.setOnAction 电话

exit.setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent event) {
        System.exit(0);
    }
}); // NOT })}

【讨论】:

  • 我什至建议“下一行”最后三个字符以进行适当的缩进。
【解决方案3】:

})} 替换为}});。您必须按照打开它们的相反顺序关闭块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2016-06-15
    • 2022-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多