在 JavaFX 中设置 stackPane 大小

Set stackPane size in JavaFX

我想要一个堆栈窗格来模拟全高清显示,即 1920x1080。

为此,我使用的是 640x360,它遵循相同的比例,然后我使用以下方法缩放 stackPane(称为 "screen")内的图像节点:

image.getWidth()/(fullWidth/screen.getWidth())), 

image.getHeight()/(fullHeight/screen.getHeight()))

这很好用,唯一的问题是我无法设置 "screen" 的大小,因此它在底部和顶部保留了大黑条。

正如您在下图中看到的那样,"screen" 周围有一个白色边框,以便更容易注意到黑条。

screen snapshot

下面是创建堆栈窗格并处理它的代码:

DisplayPane 构造方法

public DisplayPane(TemporalViewPane temporalViewPane, SteveMenuBar steveMenuBar, SpatialTemporalView spatialTemporalView){

        setId("display-pane");

        screen = new StackPane();
        screen.setId("screen-pane");


        controlButtonPane = new ControlButtonPane(screen, temporalViewPane, steveMenuBar, spatialTemporalView);


        setCenter(screen);
        setBottom(controlButtonPane);

    }

ControlButtonPane 构造函数class 方法:

public ControlButtonPane(StackPane screen, TemporalViewPane temporalViewPane,SteveMenuBar steveMenuBar, SpatialTemporalView spatialTemporalView){

    fullHDLabel = new Label("FullHD");
    fullHDLabel.setPadding(new Insets(5,5,5,5));   
    screen.getChildren().add(fullHDLabel);

    setId("control-button-pane");
    this.steveMenuBar = steveMenuBar;
    this.screen = screen;
    this.screen.setAlignment(Pos.TOP_LEFT);
    this.temporalViewPane = temporalViewPane;
    this.spatialTemporalView = spatialTemporalView;
    this.webView = new WebView();

    createButtons();
    setLeft(fullButtonPane);
    setLeft(fullHDLabel);
    setCenter(centerButtonPane);
    setRight(refreshButtonPane);

    createListeners();
    createButtonActions();


}

CreateButtons 方法:

public void createButtons(){


    run = new Button();
    run.setDisable(true);
    run.setId("run-button");
    run.setTooltip(new Tooltip(Language.translate("run")));

    play = new Button();
    play.setDisable(true);
    play.setId("play-button");
    play.setTooltip(new Tooltip(Language.translate("play")));

    pause = new Button();
    pause.setDisable(true);
    pause.setId("pause-button");
    pause.setTooltip(new Tooltip(Language.translate("pause")));

    stop = new Button();
    stop.setDisable(true);
    stop.setId("stop-button");
    stop.setTooltip(new Tooltip(Language.translate("stop")));       

    centerButtonPane = new HBox();
    centerButtonPane.setId("center-button-pane");
    centerButtonPane.getChildren().add(run);
    centerButtonPane.getChildren().add(play);
    centerButtonPane.getChildren().add(pause);
    centerButtonPane.getChildren().add(stop);


}

CreateListeners 方法:

private void createListeners(){


    screen.widthProperty().addListener((obs, oldVal, newVal) -> {
        if(newVal != oldVal){
            screen.minHeightProperty().bind(screen.widthProperty().multiply(0.565));
            screen.maxHeightProperty().bind(screen.widthProperty().multiply(0.565));
            System.out.println("NEW WIDTH OF SCREEN IS: "+newVal);
        }

    });

    screen.heightProperty().addListener((obs, oldVal, newVal) -> {
        if(newVal != oldVal){
            screen.minHeightProperty().bind(screen.widthProperty().multiply(0.565));
            screen.maxHeightProperty().bind(screen.widthProperty().multiply(0.565));
            System.out.println("NEW HEIGHT OF SCREEN IS: "+newVal);
        }

    });

下面是我想要实现的。目前,我必须 运行 应用程序,然后拖动堆栈窗格的一侧以调整其应有的大小。

how it should be snapshot

我已经尝试了所有设置 min/max/pref height/width,但仍然无法实现我的目标。我不知道还能做什么。

任何帮助都会很棒。

问题是我将高度绑定到宽度,而实际上它应该是相反的,因为宽度可以调整得更大而不会浪费屏幕 space。

所以我将绑定更改为:

private void createListeners(){


screen.widthProperty().addListener((obs, oldVal, newVal) -> {
    if(newVal != oldVal){
        screen.minWidthProperty().bind(screen.heightProperty().multiply(1.77778));
        screen.maxWidthProperty().bind(screen.heightProperty().multiply(1.77778));
        System.out.println("NEW WIDTH OF SCREEN IS: "+newVal);
    }

});

screen.heightProperty().addListener((obs, oldVal, newVal) -> {
    if(newVal != oldVal){
        screen.minWidthProperty().bind(screen.heightProperty().multiply(1.77778));
        screen.maxWidthProperty().bind(screen.heightProperty().multiply(1.77778));
        System.out.println("NEW HEIGHT OF SCREEN IS: "+newVal);
    }

});
}