1

我试图完成简单的任务,如果选中复选框,则舞台可以调整大小,如果未选中复选框,则无法调整大小。

代码 :

 checkBox.selectedProperty().bindBidirectional(stage.resizableProperty());

上面的代码没有按预期工作!,我可以使用下面的代码得到预期的结果

  checkBox.selectedProperty().addListener(new ChangeListener() {
         @Override
         public void changed(ObservableValue ov, Object t, Object t1) {
            stage.setResizable(checkBox.selectedProperty().getValue());
           }
      });

但想知道为什么 bindBidirectional 代码不起作用?

4

2 回答 2

3

This is a bug in the Stage class that is fixed in JavaFX 8.0 (which is available in the current Java 8.0 EAP release).

Note that the resizableProperty is actually correctly set when binded bidirectional, the set value has only no effect.

My testing:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class StageResizableTest extends Application{

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(final Stage stage) throws Exception {
    Button button = new Button("resize");

    button.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        stage.setResizable(true);
      }
    });

    Button button2 = new Button("not resize");

    button2.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        stage.setResizable(false);
      }
    });

    CheckBox checkBox = new CheckBox("Resizable");

    checkBox.selectedProperty().bindBidirectional(stage.resizableProperty());

    checkBox.selectedProperty().addListener(new ChangeListener<Boolean>() {
      @Override
      public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean2) {
        System.out.println("checkbox changed from "+aBoolean+" to "+aBoolean2);
      }
    });

    stage.resizableProperty().addListener(new ChangeListener<Boolean>() {
      @Override
      public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean2) {
        System.out.println("stage resizable changed from "+aBoolean+" to "+aBoolean2);
      }
    });

    Button button3 = new Button("request stage resizable");

    button3.setOnAction(new EventHandler<ActionEvent>() {
      @Override
      public void handle(ActionEvent actionEvent) {
        System.out.println(stage.resizableProperty().get());
      }
    });

    VBox box = new VBox();

    box.getChildren().addAll(checkBox, button, button2, button3);

    Scene scene = new Scene(box, 500, 500);

    stage.setScene(scene);

    stage.show();
  }
}
于 2013-01-14T08:59:02.330 回答
0

是的,我也注意到了这一点......不幸的是,绑定属性使得在你的事件处理程序中完成的簿记变得不必要(并且可能有更少的错误!)到目前为止,我发现编写你自己的事件处理程序,如上面的例子似乎是现在唯一要做的事情。现在我自己发现了这一点——并在这里得到了验证——这让我有点困扰,关于这个主题的书籍没有提到这个错误。例如,我首先通过一个取自Pro JavaFX 2: A Definitive Guide to Rich Clients with Java Technology的示例来玩弄这个概念。第 2 章的第一个例子涉及到这个绑定,甚至做了一个特别的说明:

提示:绑定的属性不能显式设置。在代码片段之前的代码中,可调整大小的属性是在下一行绑定属性之前使用 setResizable() 方法设置的。

您可能会认为,经过麻烦告诉我们这一点,作者也会知道这个错误。谢天谢地,这样的地方有助于保持我们的理智;-)

于 2013-07-04T19:50:24.473 回答