Java FX out of the window screen

牧云@^-^@ 提交于 2019-12-25 08:23:36

问题


I wanna it will be okay when the number variables is changed, but when the are increased the button goes out from the window. How to fix it? Also how to put the bar down to the level of "10$", so they will be in the same row?

Before :

After :

Here is my code :

VBox vboxBottom = new VBox();
HBox hboxBottomElements = new HBox(15);
HBox hboxBottomMain = new HBox(0);

Region region = new Region();
region.setPrefWidth(500);

hboxBottomElements.getChildren().addAll(visaLabel, separator2, adLabel, separator3, governRelationStatus, separator4, region, next);
hboxBottomElements.setPadding(new Insets(5));

vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));

vboxBottom.setPadding(new Insets(3));

hboxBottomMain.getChildren().addAll(new Group(moneyBox), vboxBottom);
hboxBottomMain.setPadding(new Insets(3));
layout.setBottom(hboxBottomMain);

回答1:


By using a Group here

vboxBottom.getChildren().addAll(separator1, new Group(hboxBottomElements));

you're creating a layout structure that resizes hboxBottomElements to it's prefered size independent of the space available.
HBox simply moves elements out the right side of it's bounds, if the space available does not suffice. This means if the Group containing moneyBox grows, the Button is moved out of the HBox...

The following simpler example demonstrates the behavior:

@Override
public void start(Stage primaryStage) {
    Button btn = new Button("Do something");
    HBox.setHgrow(btn, Priority.NEVER);
    btn.setMinWidth(Region.USE_PREF_SIZE);
    Region filler = new Region();
    filler.setPrefWidth(100);
    HBox.setHgrow(filler, Priority.ALWAYS);
    Rectangle rect = new Rectangle(200, 50);
    HBox hBox = new HBox(rect, filler, btn);

    Scene scene = new Scene(hBox);

    primaryStage.setScene(scene);
    primaryStage.show();
}

This will resize filler to make the HBox fit the window.

Now replace

Scene scene = new Scene(hBox);

with

Scene scene = new Scene(new Group(hBox));

and the Button will be moved out of the window...



来源:https://stackoverflow.com/questions/42599738/java-fx-out-of-the-window-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!