Vaadin - 中心定制组件

时间:2015-02-17 09:49:55

标签: vaadin vaadin7

使用Grails 2.3.9和Vaadin插件7.3.9

class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        VerticalLayout layout = new VerticalLayout()
        layout.setMargin(true)

        SignInForm signInForm = new SignInForm()

        layout.addComponent(signInForm)

        layout.setComponentAlignment(signInForm, Alignment.MIDDLE_CENTER)

        layout.setSizeFull()
        setContent(layout)

    }
}

自定义组件

class SignInForm extends CustomComponent {
    Panel p = new Panel()

    public SignInForm() {
        p.setSizeUndefined()

        Label label = new Label("test");
        p.setContent(label);

        setCompositionRoot(p);
    }
}

它的外观如下:

enter image description here

如何将自定义组件水平居中?

2 个答案:

答案 0 :(得分:5)

将自定义组件放在vetical布局中。设置自定义组件的大小未定义。设置垂直布局的大小为&将它与中心对齐。

public void init(VaadinRequest request) {

    VerticalLayout vLayout = new VerticalLayout();
    SignInComponent signInComponent = new SignInComponent();
    vLayout.addComponent(signInComponent);
    vLayout.setSizeFull();
    vLayout.setComponentAlignment(signInComponent, Alignment.MIDDLE_CENTER);
    this.setContent(vLayout);

class SignInComponent extends CustomComponent  {

    public SignInComponent() {
        Panel p = new Panel();
        p.setSizeUndefined();
        Label label = new Label("test");
        p.setContent(label);
        this.setSizeUndefined();
        this.setCompositionRoot(p);
    }
}

使用面板而不是自定义布局:

public void init(VaadinRequest request) {

    VerticalLayout vLayout = new VerticalLayout();
    SignInPanel signInPanel = new SignInPanel();
    vLayout.addComponent(signInPanel);
    vLayout.setSizeFull();
    vLayout.setComponentAlignment(signInPanel, Alignment.MIDDLE_CENTER);
    this.setContent(vLayout);

}

class SignInPanel extends Panel  {

    public SignInPanel() {
        this.setSizeUndefined();
        Label label = new Label("test");
        this.setContent(label);
    }
}

两者的代码输出: enter image description here

答案 1 :(得分:1)

如果有人需要 - 如何使用最少的UI类使用自定义组件(需要导航器组件):

组件:

public class LoginPanel extends Panel {

private TextField loginField;

private PasswordField passField;

private Button signInBtn;

public LoginPanel(){
    initComponents();
    buildLoginPanel();
}

private void initComponents(){
<omitted>
}
public void buildLoginPanel(){
<omitted>
}

使用组件的视图:

public class LoginView extends VerticalLayout implements View {

    private LoginPanel loginPanel;

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {

    }

    public LoginView(LoginPanel loginPanel){
        setSizeFull();
        addComponent(loginPanel);
        setComponentAlignment(loginPanel,Alignment.MIDDLE_CENTER);
    }
}

UI入口点:

 @Override
 protected void init(VaadinRequest request) {
        Navigator navi = new Navigator(UI.getCurrent(),this);
        navi.addProvider(viewProvider);
        navi.navigateTo("login");
   }

执行此操作后的P.S - 您的组件将被集中

P.S.S如果有人想要使用Navigator,只需删除所有地方的视图而不是navigator.navaigateTo(...)使用setContent(new LoginView())或类似的东西

希望有人帮忙