我通过观看教程来对snakeandladder进行编码,我想实现一个按钮,该按钮可打开第二个窗口,您可以在其中更改玩家的名字。 我在主类中有两个名称分别为name1和name2的变量,我希望在按下“提交”按钮时更改它们。 值会更改,但在某种程度上会从“更改名称窗口”变回主窗口后重置。
//All of that code is in my Main Class
//names for player1 and 2
private String name1 = "Player 1";
private String name2 = "Player 2";
//e.g. if player1 won I want to show his name + won
if(player1XPos < 40 || player1YPos < 40){
player1XPos = 40;
player1YPos = 40;
randResult.setText(name1 +" won");
gameStart = false;
gameButton.setText("Start Game");
}
// getters and setters for name1, name 2
public void setName1 (String value){
this.name1 = value;
}
public void setName2 (String value){
this.name2 = value;
}
public String getName1(){
return this.name1;
}
public String getName2(){
return this.name2;
}
//All of this Code is in the Change Names Controller Class
public class ChangeNamesController {
DiceRoleSnake diceRoleSnake = new DiceRoleSnake();
@FXML
public TextField tfName1;
@FXML
public TextField tfName2;
@FXML
public Button submitBtn;
//on Action SubmitBtn
@FXML
public void onSubmitBtn(ActionEvent event){
String value1 = tfName1.getText();
String value2 = tfName2.getText();
if (!value1.equals("") || !value2.equals("")){
diceRoleSnake.setName1(value1);
diceRoleSnake.setName2(value2);
((Node)(event.getSource())).getScene().getWindow().hide();
//just to check if it worked
System.out.println(diceRoleSnake.getName1());
System.out.println(diceRoleSnake.getName2());
}
}
}