在JavaFX中设置stackPane大小

时间:2018-06-30 13:29:51

标签: java javafx size pane

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

为此,我使用的640x360比例相同,然后使用以下方法缩放stackPane(称为“屏幕”)中的图像节点:

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

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

这很有效,唯一的问题是我无法设置“屏幕”的大小,因此它在底部和顶部都保留了大黑条。

您可以在“屏幕”下方的图片中看到,在其周围有一个白色边框,使黑色条形更易于观察。

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构造函数类方法:

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

我尝试了所有设置的最小/最大/首选高度/宽度,但仍然无法实现我的目标。我不知道该怎么办。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

问题是我在将高度与宽度绑定时应该将高度与宽度绑定,因为可以将宽度调整为更大的尺寸而不会浪费屏幕空间。

所以我将绑定更改为:

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

});
}