这样的行为是 javafx2 绑定中的错误吗?独立应用程序本身总是自我告知自己的状态(xPosition、yPosition、高度、宽度等),这似乎是合乎逻辑的。我正在阅读“Pro JavaFX 2 A Definitive Guide to Rich Clients with Java Technology”一书,一些代码示例根本不起作用。注意:我在这里使用的代码是我的。(这本书有关系吗?)
package simplejavafxbindingquestion;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.Label;
import javafx.scene.control.LabelBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.layout.PaneBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
public class SimpleJavaFXBindingQuestion extends Application {
@Override
public void start(final Stage stage) {
Text textBoundStageHeight;
Text textRequestedStageHeight;
final Label labelRequestedStageHeight;
VBox vBox;
HBox hBox;
Button btnRequest;
Pane sceneRoot = PaneBuilder.create()
.children(
vBox = VBoxBuilder.create()
.spacing(10)
.children(
textBoundStageHeight = TextBuilder.create()
.textOrigin(VPos.TOP)
.font(Font.font("Ariel", 20))
.build(),
hBox = HBoxBuilder.create()
.children(
textRequestedStageHeight = TextBuilder.create()
.textOrigin(VPos.TOP)
.font(Font.font("Ariel", 20))
.text("requested stage height: ")
.build(),
labelRequestedStageHeight = LabelBuilder.create()
.font(Font.font("Ariel", 20))
.text("Unknown")
.build()
)
.build(),
btnRequest = ButtonBuilder.create()
.text("Request height!")
.font(Font.font("Ariel", 15))
.onAction(new EventHandler<javafx.event.ActionEvent>() {
@Override
public void handle(javafx.event.ActionEvent event) {
labelRequestedStageHeight.setText("" + stage.getHeight());
}
})
.build()
)
.build()
)
.build();
textBoundStageHeight.textProperty()
.bind(new SimpleStringProperty("bound stage height: ")
.concat(stage.getHeight()));
Scene scene = SceneBuilder.create()
.root(sceneRoot)
.build();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}