简体   繁体   English

在JavaFX中向窗口动态添加元素

[英]Dynamically add elements to window in JavaFX

I would like to have a window that shows an image. 我想要一个显示图像的窗口。 That is the main purpose of the window. 这是窗口的主要目的。 However it should be possible to also have controls on the top. 但是,应该也可以在顶部设置控件。 The number is not known beforehand. 该号码事先未知。 Could be 3 or 15. They should just pile up there for now. 可能是3或15。它们应该现在就堆在那里。 So the upper part grows and the image below is just being pushed down. 因此,上部会增大,而下面的图像只会被向下推。

在此处输入图片说明

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();
}

This code barely works. 这段代码几乎行不通。 I have to add the width and height manually because the scene or the stage doesn't look for anything to fit to. 我必须手动添加宽度和高度,因为场景或舞台找不到适合的东西。 And when I am adding buttons later to the HBox on the top the window doesn't increase in size, neither does the HBox (height stays at 0). 当我稍后在顶部的HBox添加按钮时,窗口的大小不会增加, HBox也不会增加(高度保持为0)。 Only the image gets pushed down it is not fully visible anymore. 只有图像被按下,它不再完全可见。

How would I go about this instead? 我将如何处理呢?

You should use the HBox.setHgrow method on each of the children of controlBox . 您应该使用HBox.setHgrow对每个孩子的方法controlBox

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

This will align the buttons next to each other, reducing the sizes so that all of them fit in one line and they fill the available space. 这将使按钮彼此相邻对齐,从而减小尺寸,以使所有按钮都适合一行,并填充可用空间。

JavaFX nodes are dynamically re-sizable ie the child will fill the space provided by parent and Parent will expand in accordance to the minimum space the child needs. JavaFX节点可动态调整大小,即子级将填充父级提供的空间,而父级将根据子级所需的最小空间进行扩展。

I don't face issue that have been raised by you while trying to add a HBox to a BorderPane . 在尝试将HBox添加到BorderPane ,我不会遇到您提出的问题。 The BorderPane height increased (more than the image height) after the HBox got added to it. HBox添加到边框后, BorderPane高度增加(超过图像高度)。 In case you want to see if the Image gets pushed down, try replacing a VBox to the HBox. 如果要查看图像是否被下推,请尝试将VBox替换为HBox。

A simple example where I use an image of 365 and a HBox of 26 , which result in a BorderPane of 391 height 一个简单的示例,其中我使用365的图像和HBox的26 ,导致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);
    }
}

Output on Console 在控制台上输出

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

For the window to grow as the content inside the window expands I did like this(looked but haven't found another solution) 为了使窗口随着窗口内内容的扩展而增长,我这样做是这样的(看起来但是没有找到其他解决方案)

In this case a new window is open from MainController and this window contents can grow and I want this new window to grow with it, so in the controller for the new window I add a listener... 在这种情况下,将从MainController中打开一个新窗口,并且该窗口内容可以增长,并且我希望这个新窗口随其增长,因此在新窗口的控制器中,我添加了一个侦听器...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM