JavaFX-清除我的组合框集';这是我的听众的问题,我该如何解决?
正如您在下面的代码中看到的,我有一个问题,当我更改我的cbGameType组合框中的值时,它会清除cbPlayerCount组合框,这会触发cbPlayerCount侦听器并抛出一个错误。这会导致ComboBox无响应,并且在设置另一个值之前不会更改cbGameType ComboBox值。设置另一个值后,程序再次开始正常工作,我将如何修复此问题 编辑:顺便说一下,引发此问题的特定代码如下:JavaFX-清除我的组合框集';这是我的听众的问题,我该如何解决?,java,combobox,listener,javafx-8,Java,Combobox,Listener,Javafx 8,正如您在下面的代码中看到的,我有一个问题,当我更改我的cbGameType组合框中的值时,它会清除cbPlayerCount组合框,这会触发cbPlayerCount侦听器并抛出一个错误。这会导致ComboBox无响应,并且在设置另一个值之前不会更改cbGameType ComboBox值。设置另一个值后,程序再次开始正常工作,我将如何修复此问题 编辑:顺便说一下,引发此问题的特定代码如下: cbPlayerCount.getItems().clear(); 编辑:整个听众都在这里 // Se
cbPlayerCount.getItems().clear();
编辑:整个听众都在这里
// Sets additional player for specific game type
cbGameType.valueProperty().addListener(e -> {
cbPlayerCount.getItems().clear();
if (!cbGameType.getValue().equals("Texas Hold 'Em")) {
cbPlayerCount.getItems().addAll(olPlayerCount.subList(0, 6));
cbPlayerCount.setValue(olPlayerCount.get(5));
} else {
cbPlayerCount.getItems().addAll(olPlayerCount.subList(1, 5));
cbPlayerCount.setValue(olPlayerCount.get(1));
}
});
编辑:下面是清除组合框时触发的cbPlayerCount组合框侦听器的代码。我假设正在发生的事情是,它正在尝试else语句来设置新值,但它还没有设置值
// Set AI count specifically based on the number of players/game
cbPlayerCount.valueProperty().addListener(e -> {
cbAICount.getItems().clear();
if (cbGameType.getValue().equals("Texas Hold 'Em")) {
if (cbPlayerCount.getValue().equals("2 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 1));
cbAICount.setValue(olAI.get(0));
} else if (cbPlayerCount.getValue().equals("3 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 2));
cbAICount.setValue(olAI.get(1));
} else if (cbPlayerCount.getValue().equals("4 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 3));
cbAICount.setValue(olAI.get(2));
} else if (cbPlayerCount.getValue().equals("5 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 4));
cbAICount.setValue(olAI.get(3));
} else if (cbPlayerCount.getValue().equals("6 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 5));
cbAICount.setValue(olAI.get(4));
}
} else {
if (cbPlayerCount.getValue().equals("1 Player") ||
cbPlayerCount.getValue().equals("2 Players")) {
cbAICount.setVisible(false);
} else if (cbPlayerCount.getValue().equals("3 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 1));
cbAICount.setValue(olAI.get(0));
} else if (cbPlayerCount.getValue().equals("4 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 2));
cbAICount.setValue(olAI.get(1));
} else if (cbPlayerCount.getValue().equals("5 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 3));
cbAICount.setValue(olAI.get(2));
} else if (cbPlayerCount.getValue().equals("6 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 4));
cbAICount.setValue(olAI.get(3));
}
}
});
下面是代码的其余部分
Stage newGameStage = new Stage();
VBox pane = new VBox(5);
// Label and ComboBox for gameType selection
BorderPane gameTypePane = new BorderPane();
ComboBox<String> cbGameType = new ComboBox<>();
cbGameType.setPrefWidth(160);
cbGameType.getItems().addAll("Texas Hold 'Em", "7-Card Stud",
"Omaha", "5-Card Draw", "High/Low Chicago", "Follow the Queen",
"BlackJack");
cbGameType.setValue("Texas Hold 'Em");
Label lbGameType = new Label("Game Type:");
lbGameType.setAlignment(Pos.CENTER_LEFT);
gameTypePane.setLeft(lbGameType);
gameTypePane.setRight(cbGameType);
BorderPane.setAlignment(cbGameType, Pos.CENTER_RIGHT);
pane.getChildren().add(gameTypePane);
// Label and ComboBox for number of Players
BorderPane playerCountPane = new BorderPane();
ObservableList<String> olPlayerCount =
FXCollections.observableArrayList( "1 Player", "2 Players",
"3 Players", "4 Players", "5 Players", "6 Players");
ComboBox<String> cbPlayerCount = new ComboBox<>();
cbPlayerCount.getItems().addAll(olPlayerCount.subList(1, 6));
cbPlayerCount.setValue(olPlayerCount.get(5));
Label lbPlayerCount = new Label("Select Players");
lbPlayerCount.setAlignment(Pos.CENTER_LEFT);
playerCountPane.setLeft(lbPlayerCount);
playerCountPane.setRight(cbPlayerCount);
BorderPane.setAlignment(cbPlayerCount, Pos.CENTER_RIGHT);
pane.getChildren().add(playerCountPane);
// Label and ComboBox for selecting the number of AI Players
BorderPane aiCountPane = new BorderPane();
ObservableList<String> olAI = FXCollections.observableArrayList(
"1 AI", "2 AI", "3 AI",
"4 AI", "5 AI");
ComboBox<String> cbAICount = new ComboBox<>();
cbAICount.getItems().addAll(olAI.subList(0, 5));
cbAICount.setValue(olAI.get(4));
Label lbAICount = new Label("How many AI: ");
lbAICount.setAlignment(Pos.CENTER_LEFT);
aiCountPane.setLeft(lbAICount);
aiCountPane.setRight(cbAICount);
BorderPane.setAlignment(cbAICount, Pos.CENTER_RIGHT);
pane.getChildren().add(aiCountPane);
// Sets additional player for specific game type
cbGameType.valueProperty().addListener(e -> {
cbPlayerCount.getItems().clear();
if (!cbGameType.getValue().equals("Texas Hold 'Em")) {
cbPlayerCount.getItems().addAll(olPlayerCount.subList(0, 6));
cbPlayerCount.setValue(olPlayerCount.get(5));
} else {
cbPlayerCount.getItems().addAll(olPlayerCount.subList(1, 5));
cbPlayerCount.setValue(olPlayerCount.get(1));
}
});
// Set AI count specifically based on the number of players/game
cbPlayerCount.valueProperty().addListener(e -> {
cbAICount.getItems().clear();
if (cbGameType.getValue().equals("Texas Hold 'Em")) {
if (cbPlayerCount.getValue().equals("2 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 1));
cbAICount.setValue(olAI.get(0));
} else if (cbPlayerCount.getValue().equals("3 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 2));
cbAICount.setValue(olAI.get(1));
} else if (cbPlayerCount.getValue().equals("4 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 3));
cbAICount.setValue(olAI.get(2));
} else if (cbPlayerCount.getValue().equals("5 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 4));
cbAICount.setValue(olAI.get(3));
} else if (cbPlayerCount.getValue().equals("6 Players")) {
cbAICount.getItems().addAll(olAI.subList(0, 5));
cbAICount.setValue(olAI.get(4));
}
} else {
if (cbPlayerCount.getValue().equals("1 Player") ||
cbPlayerCount.getValue().equals("2 Players")) {
cbAICount.setVisible(false);
} else if (cbPlayerCount.getValue().equals("3 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 1));
cbAICount.setValue(olAI.get(0));
} else if (cbPlayerCount.getValue().equals("4 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 2));
cbAICount.setValue(olAI.get(1));
} else if (cbPlayerCount.getValue().equals("5 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 3));
cbAICount.setValue(olAI.get(2));
} else if (cbPlayerCount.getValue().equals("6 Players")) {
cbAICount.setVisible(true);
cbAICount.getItems().addAll(olAI.subList(0, 4));
cbAICount.setValue(olAI.get(3));
}
}
});
清除组合框时,
值
属性设置为null
。因此像cbPlayerCount.getValue().equals(“2个玩家”)
这样的语句将抛出空指针异常(试图对空引用调用equals(…)
方法)
根据要实现的逻辑,您有两个选项。您可以使用进行比较,这将对每个参数执行适当的空检查:
if (Objects.equals(cbPlayerCount.getValue(), "2 Players")) {
//...
}
或者你可以把你的听众的内容用
if (cbPlayerCount.getValue() != null) {
//...
}
对于第二个选项,如果没有选择任何值,您可能需要考虑是否需要对其他组合框进行一些更改。James\d,我在调试器中设置了一个断点,并意识到您所说的。基本上,在清除cbPlayerCount时,它从cbGameType侦听器跳转到cbPlayerCount侦听器。然后,它试图对cbPlayerCount执行“getvalue.equals()”方法,正如您指出的,该方法为null。之后,它将跳回cbGameType侦听器中的最后一行,并继续添加和设置值。因此,正如您在上文中所述,我最终只需要设置一个null条件就可以解决这个问题。再次感谢@James\u D
if (cbPlayerCount.getValue() != null) {
//...
}