【问题标题】:Buttons are not clickable inside a vertical Layout Vaadin?垂直布局 Vaadin 中的按钮不可点击?
【发布时间】:2015-02-12 23:32:20
【问题描述】:

大家好,我有这个布局:

这是 MainLayout 类:

public class MainLayout extends VerticalLayout {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private VerticalLayout upperSection = new VerticalLayout();
    private HorizontalSplitPanel lowerSection = new HorizontalSplitPanel();
    private VerticalLayout menuLayout = new VerticalLayout();
    private VerticalLayout contentLayout = new VerticalLayout();

    public MainLayout() {
        upperSection.addComponent(new Label("Header"));
        menuLayout.addComponent(new Label("Menu"));
        contentLayout.addComponent(new Label("Content"));
        lowerSection.addComponent(menuLayout);
        lowerSection.addComponent(contentLayout);
        addComponent(upperSection);
        addComponent(lowerSection);
        showBorders();

        setSizeFull();
        lowerSection.setSizeFull();
        // menuLayout.setSizeFull();
        contentLayout.setSizeFull();
        setExpandRatio(lowerSection, 1);

        //lowerSection.setSplitPosition(30);
    }

    private void showBorders() {
        String style = "v-ddwrapper-over";
        setStyleName(style);
        upperSection.setStyleName(style);
        lowerSection.setStyleName(style);
        menuLayout.setStyleName(style + "-menu");
        contentLayout.setStyleName(style + "-content");
    }

    @SuppressWarnings("serial")
    public void addMenuOption(String caption, final Component component) {
        Button button = new Button(caption);
        menuLayout.addComponent(button);

        button.addClickListener(new ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                contentLayout.removeAllComponents();
                contentLayout.addComponent(component);
            }
        });
    }
}

这个布局类扩展了 VerticalLayout 并构造了布局的基本结构,addMenuOption 方法在左侧菜单列中添加了一个按钮,并为其添加了一个点击监听器,这样当用户点击该按钮时,右侧的内容布局应该将其内容从当前切换到与按钮绑定的内容,现在在 UI 的 init 方法中:

protected void init(VaadinRequest request) {
        MainLayout layout = new MainLayout();
        layout.addMenuOption("Option 1", new Label("Component 1"));
        layout.addMenuOption("Option 2", new Label("Component 2"));

        setContent(layout);
}

其实我得到的结果是这样的:

但我的问题是两个按钮(选项 1、选项 2)都不可点击。

问题出在哪里?

感谢关注!

【问题讨论】:

  • 我发现问题是通过在组件中添加“v-ddwrapper-over”样式给出的。有人能解释一下为什么吗?

标签: java user-interface layout vaadin


【解决方案1】:

你是对的。向其中一个组件添加样式“v-ddwrapper-over”会使按钮不可点击。我们来看看 style.css 文件中这个样式的定义。

.appName .v-ddwrapper-over:before, .so5 .v-ddwrapper-over:after {
content: "";
position: absolute;
z-index: 10;
top: -1px;
right: -1px;
bottom: -1px;
left: -1px;
border: 0 solid #197de1;
}

重要的是带有 z-index 的第四行。这会将一个组件(更具体地说是 DOM 中的 div)带到前面,覆盖所有其他 z-index 值较小的组件(通常它们为 0)。

如果您确实需要将此样式应用于所有组件(对我来说似乎很奇怪),请考虑为具有更高 z-index 值的按钮添加其他样式。

详细了解 z-index 属性here

【讨论】:

  • 您认为这是由于 z-index 属性的原因吗?但是按钮如何在设置了 v-ddwrapper-over 的 z-indexed 布局内,所以它们在布局内不应该是可点击的吗?
  • 是的,它们是 INSIDE 布局。想象一张厨房桌子,上面放着一个苹果。你的桌子是你的布局,一个苹果是你的按钮。通常你可以抓住你的苹果。但是,如果您将 z-index 属性应用于表格(而不是按钮/苹果),则表格将位于苹果的顶部。您可以使用 Firefox 开发人员工具进行检查。打开您的 vaadin 页面并按 F12。找到“样式编辑器”选项卡。然后,使用 ctrl + f 查找引用样式。将 z-index 属性更改为负 10。看看发生了什么?按钮现在可以点击了。
猜你喜欢
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多