[尝试将数据发送和修改到Java fx中的另一个阶段时,出现空指针异常

问题描述 投票:0回答:1

嗨,我是Java尝试制作游戏的新手,所以我制作了单元格类和网格类来显示网格,您可以成功地向网格中添加星星墙或笑脸,但是当我尝试将网格发送到实际游戏页面时对于播放器,我会得到空指针异常,好像在网格中没有用户输入笑脸的单元格,依此类推,我已经解决了好几天的问题,请帮忙请注意,输入鼠标手势类游戏鼠标手势相同,因为我试图解决此问题,然后进行详细处理

这是单元格类别

package sample.CellandGrid;

import javafx.scene.layout.StackPane;

public class cell extends StackPane {

    int column;
    int row;
    boolean star;
    boolean wall;
    String player;

    public cell(int column, int row,boolean star,boolean wall,String player) {

        this.column = column;
        this.row = row;
        this.star=star;
        this.wall = wall;
        this.player= player;

        getStyleClass().add("cell");

//          Label label = new Label(this.toString());
//
//          getChildren().add(label);

        setOpacity(0.9);
    }
    public void makeStar(){
        getStyleClass().remove("cell-highlight");
        getStyleClass().add("cell-star-highlight");
    }
    public void removeStar(){
        getStyleClass().remove("cell-star-highlight");
        getStyleClass().add("cell");
    }
    public void makewall(){
        getStyleClass().remove("cell-highlight");
        getStyleClass().add("cell-wall-highlight");
    }
    public void removeWall(){
        getStyleClass().remove("cell-wall-highlight");
        getStyleClass().add("cell");
    }
    public void smileyone(){
        getStyleClass().remove("cell");
        getStyleClass().add("cell-smiley1-highlight");
    }
    public void removeSmileyone(){
        getStyleClass().remove("cell-smiley1-highlight");
        getStyleClass().add("cell");
    }
    public void smileytwo(){
        getStyleClass().remove("cell");
        getStyleClass().add("cell-smiley2-highlight");
    }
    public void removeSmileytwo(){
        getStyleClass().remove("cell-smiley2-highlight");
        getStyleClass().add("cell");
    }


    public void highlight() {
        // ensure the style is only once in the style list
        getStyleClass().remove("cell-highlight");

        // add style
        getStyleClass().add("cell-highlight");
    }

    public void unhighlight() {
        getStyleClass().remove("cell-star-highlight");
    }

    public void hoverHighlight() {
        // ensure the style is only once in the style list
        getStyleClass().remove("cell-hover-highlight");

        // add style
        getStyleClass().add("cell-hover-highlight");
    }

    public void hoverUnhighlight() {
        getStyleClass().remove("cell-hover-highlight");
    }

    public String toString() {
        return this.column + "/" + this.row;
    }
}

这是网格类

package sample.CellandGrid;

import javafx.scene.layout.Pane;

public class grid extends Pane {

    int rows;
    int columns;

    double width;
    double height;

    boolean star;
    boolean wall;

    cell [][] cells;

    public grid( int columns, int rows, double width, double height,boolean star,boolean wall) {

        this.columns = columns;
        this.rows = rows;
        this.width = width;
        this.height = height;
        this.star = star;
        this.wall = wall;

        cells = new cell[rows][columns];

    }

    /**
     * Add cell to array and to the UI.
     */
    public void add(cell cell, int column, int row) {

        cells[row][column] = cell;

        double w = width / columns;
        double h = height / rows;
        double x = w * column;
        double y = h * row;

        cell.setLayoutX(x);
        cell.setLayoutY(y);
        cell.setPrefWidth(w);
        cell.setPrefHeight(h);

        getChildren().add(cell);

    }

    public cell getCell(int column, int row) {
        return cells[row][column];
    }

    /**
     * Unhighlight all cells
     */
    public void unhighlight() {
        for( int row=0; row < rows; row++) {
            for( int col=0; col < columns; col++) {
                cells[row][col].unhighlight();
            }
        }
    }
}

这是Entrymouse gestue类

package sample.CellandGrid;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import sample.UserEnteredGrid.userGrid;

public class entryMouseGesture {
        public void Paint( Node node) {


            // that's all there is needed for hovering, the other code is just for painting
            if( true) {
                node.hoverProperty().addListener(new ChangeListener<Boolean>(){

                    @Override
                    public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                        System.out.println( observable + ": " + newValue);

                        if( newValue) {
                            ((cell) node).hoverHighlight();
                        } else {
                            ((cell) node).hoverUnhighlight();
                        }

                        for( String s: node.getStyleClass())
                            System.out.println( node + ":gooz " + s);
                    }

                });
            }

            node.setOnMousePressed( onMousePressedEventHandler);


        }

        EventHandler<MouseEvent> onMousePressedEventHandler = event -> {

            PickResult pick = event.getPickResult();
            Node node = pick.getIntersectedNode();
            outerloop :
            if (node instanceof cell) {
                cell cell = (cell) event.getSource();
                if(userGrid.button.compareTo("star")==0) {
                    if (event.isPrimaryButtonDown()) {
                        cell.makeStar();
                        cell.star=true;
                        System.out.println("check"+cell.star);
                    } else if (event.isSecondaryButtonDown()) {
                        cell.removeStar();
                        cell.star=false;
                        System.out.println("check"+cell.star);

                    }
                } else if(userGrid.button.compareTo("smiley1")==0) {

                    if (event.isPrimaryButtonDown()) {

                        if(userGrid.hasFirstPlayer==true){
                            Alert alert = new Alert(Alert.AlertType.WARNING);
                            //alert.setAlertType(Alert.AlertType.WARNING);
                            alert.setTitle("Warning");
                            alert.setHeaderText("ILLEGAL ACTION");
                            alert.setContentText("YOU HAVE PLACED THE FIRST PLAYER ALREADY!");
                            alert.showAndWait();
                            break outerloop;
                        }

                        cell.smileyone();
                        cell.player = "1";
                        userGrid.hasFirstPlayer=true;

                    } else if (event.isSecondaryButtonDown()) {
                        if(cell.player.compareTo("1")==0){
                            userGrid.hasFirstPlayer=false;
                        }
                        cell.removeSmileyone();
                        cell.player = "";
                        System.out.println("s1check");
                    }

                }else if(userGrid.button.compareTo("smiley2")==0) {
                    if (event.isPrimaryButtonDown()) {
                        if(userGrid.hasSecondPlayer==true){
                            Alert alert = new Alert(Alert.AlertType.WARNING);
                            //alert.setAlertType(Alert.AlertType.WARNING);
                            alert.setTitle("Warning");
                            alert.setHeaderText("ILLEGAL ACTION");
                            alert.setContentText("YOU HAVE PLACED THE Second PLAYER ALREADY!");
                            alert.showAndWait();
                            break outerloop;
                        }
                        cell.smileytwo();
                        cell.player = "2";
                        userGrid.hasSecondPlayer=true;
                    } else if (event.isSecondaryButtonDown()) {
                        if(cell.player.compareTo("2")==0){
                            userGrid.hasSecondPlayer=false;
                        }
                        cell.removeSmileytwo();
                        cell.player = "";
                        System.out.println("s2check");

                    }
                }else if(userGrid.button.compareTo("wall")==0) {
                    if (event.isPrimaryButtonDown()) {
                        cell.makewall();
                        cell.wall=true;
                    } else if (event.isSecondaryButtonDown()) {
                        cell.removeWall();
                        cell.wall=false;
                        System.out.println("wall: "+cell.wall);

                    }
                }
            }

        };
        EventHandler<MouseEvent> onMouseReleasedEventHandler = event -> {
        };

}

这是游戏鼠标手势类

package sample.CellandGrid;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import sample.UserEnteredGrid.userGrid;

public class gameMouseGesture {

    public void Paint( Node node) {


         //that's all there is needed for hovering, the other code is just for painting
        if( true) {
            node.hoverProperty().addListener(new ChangeListener<Boolean>(){

                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

                    System.out.println( observable + ": " + newValue);

                    if( newValue) {
                        ((cell) node).hoverHighlight();
                    } else {
                        ((cell) node).hoverUnhighlight();
                    }

                    for( String s: node.getStyleClass())
                        System.out.println( node + ":gooz " + s);
                }

            });
        }
        if (node instanceof cell){
            cell mycell =(cell) node;
            if (mycell.star==true){
                mycell.makeStar();
            }else if (mycell.wall==true){
                mycell.makewall();
            }else if (mycell.player=="1"){
                mycell.smileyone();
            }else if(mycell.player=="2"){
                mycell.smileytwo();
            }


        }



        node.setOnMousePressed( onMousePressedEventHandler);


    }

    EventHandler<MouseEvent> onMousePressedEventHandler = event -> {

        PickResult pick = event.getPickResult();
        Node node = pick.getIntersectedNode();
        outerloop :
        if (node instanceof cell) {
            cell cell = (cell) event.getSource();
            System.out.println("checked");
        }

    };
    EventHandler<MouseEvent> onMouseReleasedEventHandler = event -> {
    };

}

这是尺寸页面,用户在其中输入行的高度和宽度数,依此类推尺寸页面控制器

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import sample.CellandGrid.grid;
import sample.Data.dataModel;
import sample.CellandGrid.grid;
import sample.CellandGrid.cell;
import sample.UserEnteredGrid.userGrid;

import java.io.IOException;



public class dimensionPageController {
    @FXML
    TextField GridWidth;
    @FXML
    TextField GridHeight;
    @FXML
    TextField row;
    @FXML
    TextField column;
    @FXML
    TextField name1;
    @FXML
    TextField name2;

    Parent root;
    Stage stage;

    //public static

    public void pageDimensions() throws IOException {
        int gHeight =Integer.parseInt(GridHeight.getText());
        int gWidth = Integer.parseInt(GridHeight.getText());
        int rrow = Integer.parseInt(row.getText());
        int ccolumn = Integer.parseInt(column.getText());
        dataModel.gridHeight=gHeight;
        dataModel.gridWidth=gWidth;
        dataModel.row=rrow;
        dataModel.column=ccolumn;
        //try {
        System.out.println("Dimension Happened");
        //System.out.println( gHeight);
        System.out.println("AIMING FOR GRID");
        userGrid grid = new userGrid();
        grid.start((Stage)row.getScene().getWindow());

//            try {
//                root = FXMLLoader.load(getClass().getResource("/sample/GridInitializer.fxml"));
//                stage = (Stage) row.getScene().getWindow();
//                Scene scene = new Scene(root, DataModel.gridWidth, DataModel.gridHeight);
//                stage.setScene(scene);
//                stage.setTitle("please god ");
//                stage.show();
//            } catch (Exception e) {
//
//                System.out.println("grid initiakizer went wring /init.page controller");
//                e.printStackTrace();
//            }
//        }catch (Exception ex){
//            System.out.println("101");
//            ex.getCause();
//            System.out.println("101");
//        }
//
    }
}

尺寸页fxml文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane alignment="TOP_CENTER" hgap="10" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: rgb(38,38,38)"
          vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="sample.dimensionPageController">

    <Label alignment="TOP_CENTER" style="-fx-text-fill: rgb(255,255,255);         -fx-font-size: 20;-fx-border-color: rgb(250,3,3);-fx-border-radius: 10" text=" FirstPageInitialization " GridPane.columnIndex="2" GridPane.rowIndex="1" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="Page Width: " GridPane.columnIndex="1" GridPane.rowIndex="2" />
    <TextField fx:id="GridWidth" GridPane.columnIndex="3" GridPane.rowIndex="2" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="Page Height: " GridPane.columnIndex="1"
           GridPane.columnSpan="2" GridPane.rowIndex="3" />
    <TextField fx:id="GridHeight" GridPane.columnIndex="3" GridPane.rowIndex="3"  />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="First player Name: " GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="4" />
    <TextField fx:id="Name1" GridPane.columnIndex="3" GridPane.rowIndex="4" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="Second player Name: " GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.rowIndex="5" />
    <TextField fx:id="Name2" GridPane.columnIndex="3" GridPane.rowIndex="5" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="rows: " GridPane.columnIndex="1" GridPane.rowIndex="6" />
    <TextField fx:id="row" GridPane.columnIndex="3" GridPane.rowIndex="6" />

    <Label style="-fx-text-fill: rgb(255,255,255);" text="columns: " GridPane.columnIndex="1" GridPane.rowIndex="7" />
    <TextField fx:id="column" GridPane.columnIndex="3" GridPane.rowIndex="7" />

    <Button fx:id="ok" alignment="CENTER" onMouseClicked="#pageDimensions" prefHeight="31.0" prefWidth="187.0" style="-fx-fill: rgb(255,255,255)" text="Save" GridPane.columnIndex="3" GridPane.columnSpan="2" GridPane.hgrow="ALWAYS" GridPane.rowIndex="8" />
    <columnConstraints>
        <ColumnConstraints />
        <ColumnConstraints />
        <ColumnConstraints />
        <ColumnConstraints />
        <ColumnConstraints />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>

</GridPane>

这是用户看到网格并将图标放置在其上的部分(svg路径)usergrid类:

package sample.UserEnteredGrid;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import sample.CellandGrid.grid;
import sample.CellandGrid.cell;
import sample.CellandGrid.grid;
import sample.Data.dataModel;
import sample.CellandGrid.entryMouseGesture;
import sample.gamePageGrid.gamePageGRID;


public class userGrid extends Application {
    int rows = dataModel.row;
    int columns = dataModel.column;
    double width = dataModel.gridWidth;
    double height = dataModel.gridHeight;

    public static String button = "";
    public static boolean hasFirstPlayer = false;
    public static boolean hasSecondPlayer=false;


    static cell savedGridCells [][];

    public static grid TransitGrid;
    grid grid = new grid( columns, rows, width, height,false,false);
    @FXML
    Button starButton;

    HBox joinedRoot;

    public void start(Stage primaryStage) {
        try {
            StackPane root = new StackPane();
            Parent root2;
            // create grid


             entryMouseGesture mg = new entryMouseGesture();

            // fill grid
            for (int row = 0; row < rows; row++) {
                for (int column = 0; column < columns; column++) {

                    cell cell = new cell(column, row,false,false,"");

                    mg.Paint(cell);
                    //dataModel.datamodelCell[row][column]=cell;
                    grid.add(cell,column,row);

                }
            }

            root.getChildren().addAll(grid);
            root.setStyle("-fx-background-color: rgb(38,38,38)");
            root2= FXMLLoader.load(getClass().getResource("userGrid.fxml"));
            // create scene and stage
            joinedRoot = new HBox(root2,root);
            joinedRoot.setSpacing(10);
            System.out.println(root2.getLayoutX());
            joinedRoot.setStyle("-fx-background-color: rgb(38,38,38)");
            Scene scene2 = new Scene(new VBox(root2,root) , width, height+110);
            scene2.getStylesheets().addAll(getClass().getResource("userGrid.css").toExternalForm());

            primaryStage.setScene(scene2);
            primaryStage.show();



        } catch (Exception e) {
            System.out.println("tap");
            e.getCause();
            e.printStackTrace();
        }
    }

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

    @FXML
    public void star(){
        System.out.println("starshit");
        button="star";
    }
    @FXML
    public void smiley1(){
        System.out.println("s1shit");
        button="smiley1";
    }
    @FXML
    public void smiley2(){
        System.out.println("s2shit");
        button="smiley2";
    }
    @FXML
    public void wall(){
        System.out.println("wall shit");
        button="wall";
    }

    @FXML
    private BorderPane gamePageBorderPane;

    @FXML
    public void submit(){
        dataModel.dataModelGrid=grid;
        System.out.println("first check "+grid.getCell(1,1));
        System.out.println("check "+dataModel.dataModelGrid.getCell(1,1));
        System.out.println("game page happening");
         gamePageGRID gamePage = new gamePageGRID();
         gamePage.start(new Stage());

//        try {
//            Stage stagew;
//            Parent root;
//            stagew = (Stage) starButton.getScene().getWindow();
//            root = FXMLLoader.load(getClass().getResource("gamePage.fxml"));
//            Scene scene = new Scene(root);
//            stagew.setScene(scene);
//            stagew.setTitle("GAME WINDOW");
//
//            stagew.show();
//        }catch (Exception e){
//            System.out.println("ude");
//            e.getCause();
//            System.out.println("ude");
//            e.printStackTrace();
//        }

    }


}

用户网格CSS

.cell{
    -fx-background-color: rgb(38,38,38);
    -fx-border-color: red;
    -fx-border-width: 1px;
}
.cell-highlight {
    -fx-background-color:derive(blue,0.9);
}
.cell-hover-highlight {
    -fx-background-color:derive(green,0.9);
}
.cell-star-highlight{

    /*-fx-background-color: derive(white,1%);*/
    /*-fx-shape:"M2,2 L8,2 L2,5 L8,5 L2,8 L8,8";*/

    -fx-shape: "M 100 0 L175 200 L0 75 L200 75 L25 200 Z" ;
    -fx-fill: derive(yellow,10%);
    -fx-background-color: derive(yellow,10%);

}
.cell-smiley1-highlight{
    -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
    -fx-background-color: derive(red,10%);
    -fx-stroke: blue;
    -fx-stroke-width: 5
}
.cell-smiley2-highlight{
    -fx-background-color: derive(blue,10%);
    -fx-shape: "M2 1 h1 v1 h1 v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z";
    -fx-background-color: rgb(255,255,255);
    -fx-stroke: red;
    -fx-stroke-width: 5
}
.cell-wall-highlight{
    -fx-background-color: derive(white,1%);
    -fx-shape:"M2,2 L8,2 L2,5 L8,5 L2,8 L8,8";
}
.background{
    -fx-background-color: rgb(38,38,38);
}

用户网格fxml文件

<?xml version="1.0" encoding="UTF-8"?>
<!--<?import sample.userGrid?>-->
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.layout.GridPane?>


<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<GridPane hgap="5" stylesheets="@userGrid.css"
          xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1"
          fx:controller="sample.UserEnteredGrid.userGrid">
    <Label text="click on button Left-click to put the element Right-click to remove" GridPane.rowIndex="1"/>
    <HBox spacing="10" GridPane.rowIndex="2">
        <Button fx:id="starButton" id="starButton-highlight" onMouseClicked="#star" text="Star" ></Button>
        <Button fx:id="wallButton" id="wallButton-highlight" onMouseClicked="#wall" text="Wall" ></Button>
        <Button fx:id="smiley1Button" id="smiley1Button-highlight" onMouseClicked="#smiley1" text="smiley1" ></Button>
        <Button fx:id="smiley2Button" id="smiley2Button-highlight" onMouseClicked="#smiley2" text="smiley2" ></Button>
        <Button fx:id="submit" text="SUBMIT" onMouseClicked="#submit"/>
    </HBox>


</GridPane>

这是用户可以玩的游戏页面游戏页类

package sample.gamePageGrid;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import sample.CellandGrid.gameMouseGesture;
import sample.CellandGrid.grid;
import sample.CellandGrid.cell;
import sample.CellandGrid.grid;
import sample.Data.dataModel;
import sample.UserEnteredGrid.userGrid;


// import static sample.dimensionPageController.grid;


public class gamePageGRID extends Application{

    int rows = dataModel.row;
    int columns = dataModel.column;
    double width = dataModel.gridWidth;
    double height = dataModel.gridHeight;

    public static String button = "";
    public static boolean hasFirstPlayer = false;
    public static boolean hasSecondPlayer=false;


    static cell savedGridCells [][];

    static grid TransitGrid;
    grid mygrid = new grid( columns, rows, width, height,false,false);
    @FXML
    Button starButton;

    HBox joinedRoot;

    public void start(Stage primaryStage) {

        StackPane ggridRoot =new StackPane();

        gameMouseGesture mg = new gameMouseGesture();

        System.out.println("OKAY GAME PAGE");

        for (int row=0;row<rows;row++){
            for (int column=0;column<columns;columns++){
                cell mycell=(cell) dataModel.dataModelGrid.getCell(column,row);
                mg.Paint(mycell);
                mygrid.add(mycell,column,row);

            }
        }
        ggridRoot.getChildren().addAll(mygrid);
        Scene gameScene = new Scene(ggridRoot);
        gameScene.getStylesheets().addAll(getClass().getResource("gameGrid.css").toExternalForm());
        primaryStage.setScene(gameScene);
        primaryStage.show();
    }

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

这里是数据模型

package sample.Data;
import sample.CellandGrid.*;
public class dataModel {
    public static int gridHeight;
    public static int gridWidth;
    public static String name1;
    public static String name2;
    public static int row;
    public static int column;
    public static grid dataModelGrid;

}

这里是错误的堆栈跟踪

Caused by: java.lang.NullPointerException
    at sample.CellandGrid.gameMouseGesture.Paint(gameMouseGesture.java:19)
    at sample.gamePageGrid.gamePageGRID.start(gamePageGRID.java:57)
    at sample.UserEnteredGrid.userGrid.submit(userGrid.java:123)
    ... 41 more

而且我的项目结构看起来像这样

enter image description here

java javafx nullpointerexception grid
1个回答
0
投票
cell cell = new cell(column, row, false ,false, "");

类型和对象具有相同的名称,对我来说这似乎是一个非常糟糕的主意。将类命名为“ Cell”,将对象命名为“ cell”,然后告诉我它是否更好。

Cell cell = new Cell(column, row, false ,false, "");

该类的声明变为:

public class Cell extends StackPane {
}

它的构造函数变为:

public Cell(int column, int row, boolean star, boolean wall, String player) {
}

按照惯例,所有类名应以大写字母开头,所有对象均以小写字母开头。

© www.soinside.com 2019 - 2024. All rights reserved.