【问题标题】:How to position controls binding there position into the stage's size using the controller class in JavaFx?如何使用 JavaFx 中的控制器类将绑定在那里的控件定位到舞台大小?
【发布时间】:2016-01-22 04:22:14
【问题描述】:

我在这个链接中发现了一些类似的东西 How to call functions on the stage in JavaFX's controller file

这是我在其中一个答案中发现的

StageTrackingSample.java

public class StageTrackingSample extends Application {

  @Override public void start(final Stage stage) throws Exception {
final FXMLLoader loader = new FXMLLoader(
  getClass().getResource(
    "stagetracking.fxml"
  )
);

final Parent root = (Parent) loader.load();
final StageTrackingController controller = loader.getController();
controller.initData(stage);
stage.setScene(new Scene(root));
stage.show();
  }

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

StageTrackingController.java

public class StageTrackingController {

@FXML private Label stageX;
public void initialize() {}  
public void initData(final Stage stage) {
 stageX.textProperty().bind(
  Bindings.format(
    "(%1$.2f, %2$.2f)", 
    stage.xProperty(), 
    stage.yProperty()
    )
   );
 }
}

我想将 progressIndicator 定位在窗口的中间,所以我在我的控制器类中尝试了这个 控制器.java

 public void initInterface(Stage stage) {

       progressIndicator.layoutXProperty().bind(stage.widthProperty().divide(2));
       progressIndicator.layoutYProperty().bind(stage.heightProperty().divide(2));
 }

这在 Main.java 中

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    final FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Hello World");
    final Parent root = loader.load();
    final Controller controller = loader.getController();
    final Scene scene = new Scene(root);
    controller.initInterface(primaryStage);
    primaryStage.setScene(scene);
    primaryStage.show();

 }


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

即使我尝试将场景或锚窗格(在 fxml 文件中定义)作为参数传递给 initInterface 方法,它也不起作用,似乎绑定 progressIndicator 属性有问题

【问题讨论】:

    标签: javafx-8


    【解决方案1】:

    通过使用 layoutXProperty 和 layoutYProperty 并将它们绑定到舞台的宽度和高度,您必须尝试将其放在舞台的右下角。通过在场景中使用布局并让场景填充整个相关区域,您可以更轻松地实现这一点,JavaFX 坚持这样做。

    “来自 layoutX 属性的 Javadocs: 如果节点是托管的并且有一个Region作为其父节点,那么布局区域会根据自己的布局策略设置layoutX。如果节点不受组管理或作为父节点,则应用程序可以直接设置 layoutX 来定位它。 这意味着 LayoutX/Y 属性由父级控制(因此它应该能够“设置”它们)。但是,当您绑定它们时,它们将无法再设置,从而导致“无法设置绑定值”异常。”

    这是一个关于区域以及如何在 SceneBuilder 中随心所欲地进行布局的很好的教程。如果您不使用 SceneBuilder,我推荐它。 https://www.youtube.com/watch?v=zvgWgpGZVKc&list=PL6gx4Cwl9DGBzfXLWLSYVy8EbTdpGbUIG&index=35

    【讨论】:

    • Javadocs 基本上是在说,当节点所在的父节点存在时,不要尝试使用阶段属性来管理节点的属性,而是使用父节点的布局属性来获取空间排列。
    • 很遗憾我忘记了 divide() 方法,无论如何重点不是把东西放在中间或角落,而是如何将它的位置与窗口尺寸绑定,但我是你'是对的,而不是绑定 layoutX/YI 尝试使用 translateX/Y 并且它有效!
    • 重点是:“不要试图这样做”。使用适当的布局窗格来定位您的组件。请参阅tutorial
    • Benfares,我可以得到一个回答点然后为我的回答好吗?
    • James_D 是对的,你应该使用 vbox 之类的,然后添加一个水平增长的区域以适应舞台大小的增加。这真的很容易,而且是做事的正确方法。但我很高兴你能得到解决方案,如果你觉得我帮助回答了你的问题,我可以为答案投票吗?
    猜你喜欢
    • 2012-10-12
    • 2013-05-14
    • 2015-01-17
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    • 2012-01-27
    相关资源
    最近更新 更多