是否有可能允许元素的大小(例如,HBox)在它所属的阶段之外增长(溢出),同时仍然可见,如果是这样的话,我该如何处理?
发布于 2015-07-14 00:07:49
将窗格包裹在一个组中,该组不能由场景调整大小。在下面的示例中,如果注释掉当前root.setCenter(...)并取消注释刚添加hbox的HBox,则会将标签限制为场景的大小(因此,随着添加的标签越多,标签将越挤压越小)。
当包装在Group中时,hbox将无限增长。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class GrowingHBoxTest extends Application {
int count = 0 ;
@Override
public void start(Stage primaryStage) {
HBox hbox = new HBox();
Button button = new Button("New label");
button.setOnAction(e -> hbox.getChildren().add(new Label("Label "+(++count))));
hbox.setMaxWidth(Double.MAX_VALUE);
BorderPane root = new BorderPane();
root.setBottom(button);
// will not grow outside of scene bounds:
root.setCenter(hbox);
// will grow outside of scene bounds:
// root.setCenter(new Group(hbox));
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}https://stackoverflow.com/questions/31387703
复制相似问题