gpt4 book ai didi

java - Vaadin - 无法将组件居中 - 设计问题

转载 作者:行者123 更新时间:2023-12-02 03:08:26 24 4
gpt4 key购买 nike


我对 css 不熟悉,不幸的是我没有太多时间来研究这个,所以我选择了内置 Vaadin ValoTheme

我有一个包含两个对象的垂直布局 - 标签和面板,两个组件都应该居中。这就是我想要实现的目标:

This is what i want to archive

不幸的是,经过无数次尝试制作类似,我得到了这个

enter image description here

我的代码:

public class SplashScreen extends VerticalLayout implements View {

private static final int PANEL_WIDTH = 320;
private static final int PANEL_HEIGHT = 140;
private static final int BUTTON_WIDTH = 270;
private static final int BUTTON_HEIGHT = 70;

private ComponentHelper componentHelper;
private Panel panel;
private VerticalLayout formLayout;
private Label welcome;
private Button toLoginPage;

public SplashScreen() {
initComponents();
buildSplashView();
}

protected void initComponents() {
componentHelper = ComponentHelper.getInstance();
panel = componentHelper.createPanel("", PANEL_WIDTH, PANEL_HEIGHT);
welcome = componentHelper.createH3Label("Welcome");
formLayout = componentHelper.createVerticalLayout();
toLoginPage = componentHelper.createFriendlyButton("To Login Page", BUTTON_WIDTH, BUTTON_HEIGHT);
toLoginPage.addClickListener(this::redirect);
}

private void buildSplashView() {
addComponent(panel);
addComponent(welcome);
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
panel.setContent(formLayout);
panel.setStyleName(ValoTheme.PANEL_BORDERLESS);
setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setHeight(100, Unit.PERCENTAGE);
}

public void redirect(Button.ClickEvent event) {
try {
Thread.sleep(Constants.TRANSITION_TIME_DELAY);
getUI().getNavigator().navigateTo(ViewTokens.SIGNIN);
} catch (InterruptedException e) {
}
}

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

}
}

我做错了什么?我无法完全理解如何正确设计布局(在问这个问题之前,我读过 Vaadin 的书)

注意:“componentHelper”用于使用一些最小的 ValoTheme 样式创建对象。

感谢您的建议

更新:使用welcome.setSizeUndefined()的结果;

welcome.setSizeUndefined();

更新 2:修改了 @qtdzz 代码以重现问题:

private void buildSplashView() {
addComponent(welcome);
addComponent(text1);
addComponent(panel);
setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
setComponentAlignment(text1, Alignment.MIDDLE_CENTER);
setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setSizeFull();

panel.setSizeUndefined();
panel.setContent(formLayout);
welcome.setSizeUndefined();
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
formLayout.setSizeFull();
}

最佳答案

我认为,如果将welcome标签放在formLayout中并为其设置对齐方式会更容易。

这是我的建议,希望对您有所帮助:

private void buildSplashView() {
addComponent(panel);
setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setSizeFull();

panel.setContent(formLayout);
panel.setStyleName(ValoTheme.PANEL_BORDERLESS);

formLayout.addComponent(welcome);
welcome.setSizeUndefined();
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
formLayout.setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
formLayout.setSizeFull();
}

以下是上述代码的屏幕截图: enter image description here

更新1:根据您的代码并根据我的理解,可以通过以下方式解释一种解决方案。如果添加“text1”、“welcome”和“panel”,则如下图所示: enter image description here因此,为了使它们居中对齐,我们需要将“text1”和“panel”的expandRatio设置为0.5,将“welcome”设置为0。然后将“text1”设为Alignment.BOTTOM_CENTER,“welcome”设为Alignment.MIDDLE_CENTER,“panel”设为Alignment.TOP_CENTER,toLoginPage按钮设为Alignment.TOP_CENTER。然后 View 将如下所示: enter image description here上图代码:

private void buildSplashView() {
addComponent(text1);
addComponent(welcome);
addComponent(panel);
setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
setComponentAlignment(text1, Alignment.BOTTOM_CENTER);
setComponentAlignment(panel, Alignment.TOP_CENTER);
setSizeFull();
setExpandRatio(text1,0.5f);
setExpandRatio(panel,0.5f);

panel.setSizeUndefined();
panel.setContent(formLayout);

welcome.setSizeUndefined();
text1.setSizeUndefined();

formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.TOP_CENTER);
formLayout.setSizeFull();
}

顺便说一句,我觉得维护和理解上面的解决方案真的很难(例如,如果您想添加另一个文本,则必须将其添加在 text1 和welcome 之间)。我推荐的解决方案是使用一个包装器布局(垂直布局)来包装所有组件并将其设置为基本布局的 middle_center。解决这个问题的办法是:

 private void buildSplashView() {
wrapperLayout.addComponent(text1);//wrapperLayout is a vertical Layout
wrapperLayout.addComponent(welcome);
wrapperLayout.addComponent(panel);
wrapperLayout.setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
wrapperLayout.setComponentAlignment(text1, Alignment.MIDDLE_CENTER);
wrapperLayout.setComponentAlignment(panel, Alignment.MIDDLE_CENTER);

addComponent(wrapperLayout);
setComponentAlignment(wrapperLayout, Alignment.MIDDLE_CENTER);
setSizeFull();

panel.setSizeUndefined();
panel.setContent(formLayout);

welcome.setSizeUndefined();
text1.setSizeUndefined();

formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
formLayout.setSizeFull();
}

通过这种方法,您可以将任何组件添加到wrapperLayout中,它会自动对齐middle_center。

关于java - Vaadin - 无法将组件居中 - 设计问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41416572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com