java 用于检查布尔值的 JavaFX 侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37785689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX Listener to check for a boolean value
提问by mineshmshah
I have a game with a GUI with a couple of buttons that you press to eventually complete the game.
我有一个带有 GUI 的游戏,你可以按下几个按钮来最终完成游戏。
I have the following boolean to test:
我有以下布尔值要测试:
public boolean complete(){
if (initvalue==finalvaue)){
return true;
}
else {
return false;
}
}
I want to create a listener to check if the game is finished and to print out a message to the user. I think I need to create a SimpleBooleanProperty
for this to work. But would I need to bind it to the boolean value above?
我想创建一个侦听器来检查游戏是否完成并向用户打印一条消息。我想我需要为此创建一个SimpleBooleanProperty
。但是我需要将它绑定到上面的布尔值吗?
What would the best way to go about this?
最好的方法是什么?
回答by DVarga
You can have a BooleanProperty
called completedProperty
and you can have initvalueProperty
and finalValueProperty
also to store the points of the game, and then you can simply bind the value of the completedProperty
like
你可以有一个BooleanProperty
调用completedProperty
,你可以有initvalueProperty
,finalValueProperty
也可以存储游戏的点数,然后你可以简单地绑定completedProperty
类似的值
completedProperty.bind(initValueProperty.isEqualTo(finalValueProperty));
therefore you don't even need a method anymore.
因此,您甚至不再需要方法。
In the example I have just placed a Button
which increments the current value by one on press and I used 3 properties to continuously check the "game" state. As soon as the completedProperty
switches to "true", a Label
with text of "Game Over" appears.
在示例中,我刚刚放置了一个Button
,它在按下时将当前值增加 1,并且我使用了 3 个属性来连续检查“游戏”状态。一旦completedProperty
切换到“true”,就会出现Label
带有“Game Over”字样的文字。
Note:I have used IntegerProperty
to store the current and the final value, but you can use any Property
.
注意:我曾经使用IntegerProperty
过存储当前值和最终值,但您可以使用任何Property
.
Example:
例子:
public class Main extends Application {
private IntegerProperty initValueProperty = new SimpleIntegerProperty(0);
private IntegerProperty finalValueProperty = new SimpleIntegerProperty(10);
private BooleanProperty completedProperty = new SimpleBooleanProperty();
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
Scene scene = new Scene(root,400,400);
Button buttonPlusOne = new Button("+1");
root.setCenter(buttonPlusOne);
Text currValueText = new Text();
currValueText.textProperty().bind(initValueProperty.asString());
root.setBottom(currValueText);
// Increment current value
buttonPlusOne.setOnAction(e -> initValueProperty.set(initValueProperty.get()+1));
// Bind completed property: initValue equals finalValue
completedProperty.bind(initValueProperty.isEqualTo(finalValueProperty));
completedProperty.addListener((observable, oldValue, newValue) -> {
// Only if completed
if (newValue)
root.setTop(new Label("Game Over"));
});
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Optionally (as noted by fabian) you can even use a BooleanBinding
rather than a property to check the completed state (in this case you don't need completedProperty
), like:
可选(如fabian 所述)您甚至可以使用 aBooleanBinding
而不是属性来检查完成状态(在这种情况下您不需要completedProperty
),例如:
BooleanBinding completed = initValueProperty.isEqualTo(finalValueProperty);
completed.addListener((observable, oldValue, newValue) -> {
// Only if completed
if (newValue)
root.setTop(new Label("Game Over"));
});
To learn about properties in JavaFX this is a really good articleto start with.
要了解 JavaFX 中的属性,这是一篇非常好的入门文章。