Java 存在';这是一个适当的价值观
我遇到的问题是,用户名不会显示在我的游说窗格中。我使用了System.out.println(MainStart.player.getName());验证名称是否存在并获取正确信息。由于某种原因,它不会显示。为了尽我所能简化本例中的大量代码,例如导入和设计。这是我的问题的一个简短形式 每个窗格中都有相应的场景,这些场景都在进行场景切换,但是它们都只有几行代码,所以我将它们省略了 主流艺术Java 存在';这是一个适当的价值观,java,null,Java,Null,我遇到的问题是,用户名不会显示在我的游说窗格中。我使用了System.out.println(MainStart.player.getName());验证名称是否存在并获取正确信息。由于某种原因,它不会显示。为了尽我所能简化本例中的大量代码,例如导入和设计。这是我的问题的一个简短形式 每个窗格中都有相应的场景,这些场景都在进行场景切换,但是它们都只有几行代码,所以我将它们省略了 主流艺术 public class MainStart extends Application { publ
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;
}
}
}
内窗格
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;
}
}
新柏油烷
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");
}
}
性格
public class Character {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println(name);
}
}
哪一行正在打印null?LobbyPane/*Name/Text userName=新文本(MainStart.player.getName());/VBox*/VBox top=新的VBox();top.getChildren().addAll(用户名);返回顶部;哪一行正在打印null?LobbyPane/*Name/Text userName=新文本(MainStart.player.getName());/VBox*/VBox top=新的VBox();top.getChildren().addAll(用户名);返回顶部;
public class Character {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println(name);
}
}