当有值

时间:2017-06-30 11:58:48

标签: java null

我遇到的问题是用户名不会显示在我的LobbyPane中。我使用了System.out.println(MainStart.player.getName());验证名称是否存在并获得正确的信息。由于某种原因,它不会显示。我已经为这个例子采用了很多代码来简化我能做的最好的事情,比如导入和设计。所以这是我的问题的一个简短形式。

每个窗格都有相应的场景来追求场景切换,但是它们都是几行代码,所以我将它们排除在外。

MainStart

public class MainStart extends Application {

    public static Stage mainStage;
    public static int sceneWidth = 1050;
    public static int sceneHeight = 600;
    public static Character player = new Character();
    private static IntroScene introScene = new IntroScene();
    private static NewPlayerScene newPlayerScene = new NewPlayerScene();
    private static LobbyScene lobbyScene = new LobbyScene();

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        mainStage = primaryStage;
        mainStage.setTitle("game");
        mainStage.setResizable(false);
        mainStage.show();
        setScene("introScene");
    }

    public static void setScene(String scene) {

        switch (scene){
            case "introScene":
                mainStage.setScene(introScene);
                break;
            case "newPlayerScene":
                mainStage.setScene(newPlayerScene);
                break;
            case "lobbyScene":
                mainStage.setScene(lobbyScene);
                break;
        }
    }
}

IntroPane

public class IntroPane extends VBox {

    public IntroPane() {
        /* Main VBox */
        this.getChildren().addAll(titleText, buttons());
        this.setAlignment(Pos.CENTER);
        this.setSpacing(80);
    }

    public HBox buttons(){
        HBox buttons = new HBox();
        /* New Game Button */
        Button newGame = new Button("New Game");
        MyStyles.ButtonStyle(newGame);
        newGame.setOnMouseClicked(e->{
            MainStart.setScene("newPlayerScene");
        });

        /* Add buttons to HBox */
        buttons.getChildren().addAll(newGame);
        buttons.setSpacing(100);
        buttons.setAlignment(Pos.CENTER);
        return buttons;
    }
}

NewPlayerPane

public class NewPlayerPane extends VBox {

    private TextField userName = new TextField();

    public NewPlayerPane() {
        /* User Input */
        userName.setMaxWidth(400);
        /* Press Enter */
        userName.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
                if (keyEvent.getCode() == KeyCode.ENTER) {
                    send();
                }
            }
        });

        /* Buttons */
        Button submit = new Button("Submit");
        MyStyles.ButtonStyle(submit);
        submit.setOnAction(e->{
            send();
        });

        /* Button HBox */
        HBox buttons = new HBox(50);
        buttons.getChildren().addAll(submit);
        buttons.setAlignment(Pos.CENTER);

        /* Set */
        this.setSpacing(50);
        this.setAlignment(Pos.CENTER);
        this.getChildren().addAll(enterName, userName, buttons);
    }

    public void send() {
        String userNameText = userName.getText();
        MainStart.player.setName(userNameText);
        userName.setText("");
        MainStart.setScene("lobbyScene");
    }
}

LobbyPane

public class LobbyPane extends BorderPane{

    public LobbyPane() {
        System.out.println(MainStart.player.getName());
        this.setTop(top());
    }

    /* Top Pane */
    public VBox top() {
        /* Name */
        Text userName = new Text(MainStart.player.getName());

        /* HBox */
        HBox topSection = new HBox();

        /* VBox */
        VBox top = new VBox();
        top.getChildren().addAll(userName);

        return top;
    }
}

字符

public class Character {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println(name);
    }
}

0 个答案:

没有答案