我正在尝试仅使用Board类作为GUI,使用Game类来运行和测试游戏规则,将Pieces作为每个棋子的父类,使用Player类来指示谁来使用Java制作象棋游戏。 我首先用8 x 8 = 64个按钮填充电路板。 为了实现每个部件的有效运动,我需要获取每个按钮的地址或索引。最初我没有使用2d数组,但我认为将按钮放入2d数组并获取它们的索引来控制棋子的移动会更容易。 到目前为止,这是我尝试过的操作,但我不知道为什么会出现NullPointer错误。
我会寻求任何帮助或建议。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Board extends Application {
GridPane board = new GridPane();
Scene s = new Scene(board);
Button buttonArray[][];
private void showBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
buttonArray[i][j] = new Button();
buttonArray[i][j].setMinSize(70, 70);
if ((i + j) % 2 == 0) {
buttonArray[i][j].setStyle("-fx-background-color: #0e0f0f");
} else {
buttonArray[i][j].setStyle("-fx-background-color: #ededed");
}
buttonArray[i][j].setOnAction(this::onSelect);
board.add(buttonArray[i][j], i, j);
}
}
}
// event listener
private void onSelect(ActionEvent event) {
Button b = (Button) event.getSource();
System.out.println("get source" + b);
b.setStyle("-fx-background-color: magenta");
}
@Override
public void start(Stage stage) throws Exception {
showBoard();
stage.setScene(s);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
//错误stackTrace
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Board.showBoard(Board.java:15)
at Board.start(Board.java:45)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application Board