【问题标题】:Dynamically add elements to window in JavaFX在 JavaFX 中将元素动态添加到窗口
【发布时间】:2015-01-10 03:04:09
【问题描述】:

我想要一个显示图像的窗口。这是窗口的主要目的。但是,顶部也应该有控件。这个数字事先不知道。可能是 3 或 15。它们现在应该堆积在那里。所以上面的部分变大了,下面的图片只是被推下去了。

void createNewWindow() {
    Stage stage = new Stage();
    BorderPane pane = new BorderPane();
    ImageView imageView = new ImageView("path");
    pane.setCenter(imageView);

    HBox controlBox = new HBox(10);
    pane.setTop(controlBox);

    Scene scene = new Scene(pane);
    stage.setScene(scene);
    stage.setResizable(true);
    stage.show();
}

此代码几乎无法正常工作。我必须手动添加宽度和高度,因为场景或舞台不适合任何东西。当我稍后将按钮添加到顶部的HBox 时,窗口的大小不会增加,HBox 也不会增加(高度保持在 0)。只有图像被向下推,它不再完全可见。

我该怎么做呢?

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    您应该对controlBox 的每个孩子使用HBox.setHgrow 方法。

    // for each button
    HBox.setHgrow(child, Priority.ALWAYS);
    

    这将使按钮彼此相邻对齐,减小尺寸,使它们全部排成一行并填满可用空间。

    【讨论】:

    • 以后添加元素是不是必须重新构建UI?
    【解决方案2】:

    JavaFX 节点可动态调整大小,即子节点将填充父节点提供的空间,父节点将根据子节点所需的最小空间进行扩展。

    在尝试将HBox 添加到BorderPane 时,我没有遇到您提出的问题。在将 HBox 添加到其中后,BorderPane 的高度增加(超过图像高度)。如果您想查看 Image 是否被下推,请尝试将 VBox 替换为 HBox。

    一个简单的例子,我使用365 的图像和26 的HBox,这导致BorderPane 的高度为391

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.HBox;
    import javafx.stage.Stage;
    
    public class BorderPaneHeight extends Application{
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            BorderPane borderPane = new BorderPane();
            HBox box = new HBox(10);
            ImageView imageView = new ImageView(new Image("file:///home/itachi/Pictures/aaa.png")); // Replace with your image path
            Button button1 = new Button("Add");
            Button button2 = new Button("Add");
            box.getChildren().addAll(button1, button2);
            borderPane.setTop(box);
            borderPane.setCenter(imageView);
            Scene scene = new Scene(borderPane);
            primaryStage.setScene(scene);
            primaryStage.show();
            System.out.println("Image height : " + imageView.getImage().getHeight());
            System.out.println("Hbox height : " + box.getHeight());
            System.out.println("BorderPane Height : " + borderPane.getHeight());
        }
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    控制台输出

    Image height : 365.0
    Hbox height : 26.0
    BorderPane Height : 391.0
    

    【讨论】:

    • 在你的情况下它可以工作。但是,如果我稍后在舞台已经显示之后添加按钮,它就不会再调整大小了。
    【解决方案3】:

    为了让窗口随着窗口内内容的扩展而增长,我这样做了(看过但没有找到其他解决方案)

    在这种情况下,从 MainController 打开一个新窗口,这个窗口的内容可以增长,我希望这个新窗口随之增长,所以在新窗口的控制器中我添加了一个监听器......

        containerPane.heightProperty().addListener((observable, oldValue, newValue) -> {
            MainController.theStage.setHeight(MainController.theStage.getHeight() + (newValue.doubleValue() - oldValue.doubleValue()));
        });
    

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 2018-06-30
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多