官术网_书友最值得收藏!

Time for action – the main layout

Let's start by coding the main layout using our well known friend VerticalLayout and the new guy HorizontalLayout:

  1. Create a new Vaadin project. For this example, we will use layout-framework as project name and LayoutFrameworkUI as the UI class.
  2. Create a new Java class MainLayout.
  3. Let MainLayout extend VerticalLayout:
    public class MainLayout extends VerticalLayout {
    
    }
  4. Add layouts for upper and lower sections of MainLayout:
    public class MainLayout extends VerticalLayout {
    
      private VerticalLayout upperSection = new VerticalLayout();
      private HorizontalLayout lowerSection = new     HorizontalLayout();
      private VerticalLayout menuLayout = new VerticalLayout();
      private VerticalLayout contentLayout = new VerticalLayout();
      
    }
  5. Add the following default constructor for MainLayout:
    public MainLayout() {
      upperSection.addComponent(new Label("Header"));
      menuSection.addComponent(new Label("Menu"));
      contentSection.addComponent(new Label("Content"));
      
      lowerSection.addComponent(menuLayout);
      upperSection.addComponent(contentLayout);
      
      addComponent(upperSection);
      addComponent(lowerSection);
    }
  6. Change the init method in your LayoutFrameworkUI class to match this:
    public class LayoutFrameworkUI extends UI {
    
      protected void init(VaadinRequest request) {
        setContent(new MainLayout());
      }
    
    }
  7. Compile, deploy, and run your application.

What just happened?

Let's get real; the application doesn't look very impressive so far:

We have created our very own layout class taking advantage of VerticalLayout. So our MainLayout is a VerticalLayout too, but now it contains a VerticalLayout for the header or upper section and a HorizontalLayout for the lower section which in turn contains a menu layout and a content layout, both using VerticalLayout. Layout, layout, layout, what a tongue-twister!

We have added some labels to see how our layout works.

Note

Note that when we add components into a HorizontalLayout they are placed from left to right.

Take a look at the components tree for MainLayout (we are showing only layout components):

主站蜘蛛池模板: 云安县| 桃江县| 新邵县| 游戏| 舒城县| 诸暨市| 万载县| 临高县| 祁东县| 安义县| 镇赉县| 惠安县| 杭锦旗| 锡林郭勒盟| 义马市| 徐州市| 全椒县| 虎林市| 榆林市| 宜阳县| 伊春市| 江达县| 江陵县| 阿巴嘎旗| 东兰县| 德庆县| 扎鲁特旗| 建德市| 洛扎县| 迁安市| 双峰县| 叙永县| 思南县| 乡宁县| 临夏县| 浮梁县| 田东县| 芜湖县| 大冶市| 南岸区| 阿拉善左旗|