vaadin_demo

 

简介:主要一个登陆界面,登陆后跳转到主界面;

主界面功能是点新增,新增人员数据会保存到显示列表中;

一个很简单的demo

下面是截图

 

下面附代码:

People类:

package com.example.demo.entity;

public class People {

    /*
     * 姓名
     */
    private String name;
    
    /*
     * 地址
     */
    private String addr;
     
    /*
     * 性别
     */
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
    
}
View Code

DemoApplication类

package com.example.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.types.CommandlineJava.SysProperties;

import com.example.demo.entity.People;
import com.vaadin.Application;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.TextField;
import com.vaadin.ui.TreeTable;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;

/**
 * Main application class.
 */
@SuppressWarnings("serial")
public class DemoApplication extends Application implements ClickListener, SelectedTabChangeListener{
    
    //登录按钮 
    private Button loginButton;
    
    //功能按钮
    private Button added,show,b3;
    
    //水平布局
    private HorizontalSplitPanel horizontalSplitPanel;
    
    //网格布局
    private GridLayout grid;
    
    //表格树
    private TreeTable treetable;
    
    //提交按钮
    private Button commit;
    
    //录入界面的姓名,地址
    private TextField nameField,address;
    
    //录入界面的性别
    private ComboBox sex;
    
    //sheet页标签
    private TabSheet t;
    
    //布局
    private VerticalLayout inputLayout,showLayout;
    
    //显示人员列表
    private List<People> lists;
    
    //初始化
    public void init() {
        Window mainWindow = new Window("Demo Application");

        //先定义一个水平布局
        VerticalLayout verticalLayout = new VerticalLayout();
        
        //用户名
        TextField usernameField = new TextField("用户名");
        usernameField.setValue("");
        verticalLayout.addComponent(usernameField);
        
        //密码
        PasswordField passwordField = new PasswordField("密码");
        passwordField.setValue("");
        verticalLayout.addComponent(passwordField);
        
        //为登录按钮添加监听事件
        loginButton = new Button("登录");
        loginButton.addListener((ClickListener)this);
        
        //初始化全局变量
        t = new TabSheet();
        lists = new ArrayList<People>();
        
        //添加布局
        mainWindow.addComponent(verticalLayout);
        mainWindow.addComponent(loginButton);
        
        setMainWindow(mainWindow);
    }

    //按钮点击事件
    public void buttonClick(ClickEvent event) {
        Window mainWindow = this.getMainWindow();
        
        Button eventButton = event.getButton();
        if(eventButton == loginButton){//点击登录按钮触发事件
            mainWindow.removeAllComponents();
            showMainPage();//跳转登录后主界面,加载布局
            horizontalSplitPanel.addComponent(t);//整个过程只需加载一次
        }else if (eventButton == added){
            detailForm();//跳转个人信息录入界面
        }else if (eventButton == show) {
            detailForm2();//跳转到显示界面
        }else if(eventButton == commit){
            addDataToList();//讲添加界面的数据保存到显示列表中
        }
    }

    /**
     * 主界面
     */
    public void showMainPage(){
        Window mainWindow = this.getMainWindow();
        
        //定义水平布局
        horizontalSplitPanel = new HorizontalSplitPanel();
        horizontalSplitPanel.setSizeFull();
        horizontalSplitPanel.setHeight("650px");
        horizontalSplitPanel.setSplitPosition(20);
        
        mainWindow.addComponent(horizontalSplitPanel);
        
        //添加固定滑动监控
        CheckBox toggleLocked = new CheckBox("Splits locked",
                new Button.ClickListener() {
                    // inline click.listener
                    public void buttonClick(ClickEvent event) {
                        horizontalSplitPanel.setLocked(event.getButton().booleanValue());
                    }
                });
        toggleLocked.setImmediate(true);
        mainWindow.addComponent(toggleLocked);
        
        treePanel();//部署树形结构
    }
    
    /**
     * 左边结构
     */
    public void treePanel(){
        Accordion accordion = new Accordion();
        horizontalSplitPanel.addComponent(accordion);
        
        //功能按钮
        added = new Button("新增");
        show = new Button("显示");
        b3 = new Button("b3");
        added.addListener(this);
        show.addListener(this);
        b3.addListener(this);
        
        accordion.setHeight("100%");
        accordion.setWidth("600px");
        
        //为布局添加按钮功能
        accordion.addTab(added,"录入界面");
        accordion.addTab(show,"显示界面");
        accordion.addTab(b3,"czy3");
    }
    
    /**
     * 右边详情界面(录入界面)
     */
    public void detailForm(){
        
        if(inputLayout == null) {
            inputLayout = new VerticalLayout();
            inputLayout.setMargin(true);
            grid = new GridLayout(2,5);
            grid.setSpacing(true);
            grid.setWidth(400, Sizeable.UNITS_PIXELS);
            grid.setHeight(400, Sizeable.UNITS_PIXELS);
            
            //姓名
            nameField = new TextField("姓名");
            grid.addComponent(nameField, 0, 0);
            grid.setComponentAlignment(nameField, Alignment.TOP_LEFT);
            
            //地址
            address = new TextField("地址");
            grid.addComponent(address, 1, 0);
            
            //性别
            sex = new ComboBox("性别");
            sex.addItem("男");
            sex.addItem("女");
            grid.addComponent(sex, 0, 1);
            
            //出生日期
            PopupDateField datetime = new PopupDateField("出生日期");
            grid.addComponent(datetime, 1, 1);
            
            commit = new Button("提交");
            commit.addListener(this);
            
            grid.addComponent(commit,0,2);
            
            inputLayout.addComponent(grid);
        }
        
        if(t.getTab(inputLayout) == null) {
            t.addTab(inputLayout, "录入界面");
            t.addListener(this);
            t.getTab(inputLayout).setClosable(true);
        }
        t.setSelectedTab(inputLayout);
    }
    
    /**
     * 右边详情界面(查询界面)
     */
    public void detailForm2(){
        treetable = new TreeTable();
        if(showLayout!=null){//获取我想要移除的组件,从tabsheet页中去掉,从而实现动态刷新
            Tab tab = t.getTab(showLayout);
            t.removeTab(tab);
        }
        showLayout = new VerticalLayout();
        showLayout.setMargin(true);
        treetable.setWidth("100%");
        //编写list表格界面
        treetable.addContainerProperty("Name", String.class, "");
        treetable.addContainerProperty("Sex", String.class, "");
        treetable.addContainerProperty("addr", String.class, "");
        treetable.addItem(new Object[]{"陈泽勇","男","上海"}, null);
        if(t.getTab(showLayout) == null) {
            t.addTab(showLayout, "查询界面");
            t.addListener(this);
            t.getTab(showLayout).setClosable(true);
        }
        treetable.refreshRowCache();
        //添加数据
        if(lists != null){
            for(People people : lists){
                treetable.addItem(new Object[]{people.getName(),people.getSex(),people.getAddr()},null);
            }
        }
        showLayout.addComponent(treetable);
        t.setSelectedTab(showLayout);
        if(t.getTab(showLayout) == null) {
            t.addTab(showLayout, "查询界面");
            t.addListener(this);
            t.getTab(showLayout).setClosable(true);
        }
        t.setSelectedTab(showLayout);
    }
    
    /**
     * 添加数据到list表单中
     */
    public void addDataToList(){
        People man = new People();
        man.setName(nameField.toString());
        man.setAddr(address.toString());
        man.setSex(sex.toString());
        lists.add(man);
    }

    /**
     * 选择tabsheet事件,实现动态跳转tab页
     */
    @Override
    public void selectedTabChange(SelectedTabChangeEvent event) {
        TabSheet tabsheet = event.getTabSheet();
        Tab tab = tabsheet.getTab(tabsheet.getSelectedTab());
        if (tab != null) {
            
        }
    }
}
View Code

 

 

over...

 

posted @ 2016-10-11 16:43  ctgu_czy  阅读(403)  评论(0编辑  收藏  举报