I am using the Javafxml program to implement Scotlandyard game. I have a main windows containing the map of London and some control buttons. I need a secondary window that receives number of detectives and other initial information from user. In below code you can see that I set an action for OK button that on click my global variables change. It is good inside the "playerChooser" method, but when I print them after call. New assignments are not there.
in initialize:
@Override
public void initialize(URL url, ResourceBundle rb) {
setPnMap();
try {
playerChooser();
} catch (IOException ex) {
System.out.println("something happened: " + ex);
}
print();
}
playerChooser method:
private void playerChooser() throws IOException {
Label secondLabel = new Label("Detectives");
// ObservableList<String> options
// = FXCollections.observableArrayList(
// "3",
// "4",
// "5"
// );
ComboBox comboBox = new ComboBox();
comboBox.getItems().addAll("3", "4", "5");
comboBox.setValue("3");
CheckBox misterX = new CheckBox("MisterAI");
CheckBox detectiveSelection = new CheckBox("DetectivesAI");
misterX.setSelected(true);
Button btnOk = new Button("OK");
StackPane secondaryLayout = new StackPane();
secondaryLayout.getChildren().addAll(secondLabel, comboBox, misterX, detectiveSelection);
secondaryLayout.getChildren().add(btnOk);
btnOk.setPrefSize(75, 25);
secondLabel.setTranslateX(-65);
secondLabel.setTranslateY(-50);
comboBox.setTranslateX(30);
comboBox.setTranslateY(-50);
misterX.setTranslateX(-60);
misterX.setTranslateY(0);
detectiveSelection.setTranslateX(-50);
detectiveSelection.setTranslateY(20);
btnOk.setTranslateX(0);
btnOk.setTranslateY(50);
Scene secondScene = new Scene(secondaryLayout, 250, 150);
// New window (Stage)
Stage newWindow = new Stage();
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
// Set position of second window, related to primary window.
newWindow.setX(500);
newWindow.setY(500);
newWindow.setAlwaysOnTop(true);
newWindow.show();
btnOk.setOnAction(e -> {
if(comboBox.getValue().equals("3")){
detectives = 3;
}
else if(comboBox.getValue().equals("4")){
detectives = 4;
}
else if(comboBox.getValue().equals("5")){
detectives = 5;
}else{
detectives = 3;
}
System.out.println("Combo Box: " + detectives);
if(misterX.isSelected()){
this.MisterXAI = true;
System.out.println("mister: " + this.MisterXAI);
}else{
this.MisterXAI = false;
}
if(detectiveSelection.isSelected()){
this.DetectivesAI = true;
System.out.println("detectives: " + this.DetectivesAI);
}else{
this.DetectivesAI = false;
}
newWindow.close();
});
}
expected results:
Combo Box: 5
mister: true
detectives: true
original assignment and results:
Combo Box again: 3
mister again: true
detectives again: false
Aucun commentaire:
Enregistrer un commentaire