在页面中间显示文字 - Vaadin

cor*_*air 4 java gwt vaadin

我正在学习vaadin并且有问题在页面中间显示文本(标签)有人可以解释为什么这段代码不起作用?

Window window=new Window();

VerticalLayout root=new VerticalLayout();
root.setSizeFull();

Label c=new Label("User name");
//TextField c=new TextField("User name");

root.addComponent(c);
root.setComponentAlignment(c, Alignment.MIDDLE_CENTER);

window.setContent(root);
setMainWindow(window);
Run Code Online (Sandbox Code Playgroud)

如果要使用TextField而不是Label,那么一切都很好.那么Label有什么问题?

Hen*_*ola 9

默认情况下,Label的宽度为100%,因此Label位于居中位置,但水平占用所有可用空间.您可以通过说:

c.setWidth(null);
Run Code Online (Sandbox Code Playgroud)

要么

c.setSizeUndefined();
Run Code Online (Sandbox Code Playgroud)