Vaadin Window调整大小并自动对齐内部组件

时间:2018-04-20 21:13:49

标签: java user-interface vaadin

我有一个扩展Window的类,如下所示。我只给出与Window相关的代码。删除窗口组件代码以保持代码和问题清晰。我有一个窗口看起来像在图片中。当我调整窗口大小时,我希望窗口内的组件也会调整大小。我想有一些我缺少的API调用,以保持组件比率与窗口一起增加。或者我需要自己编程吗?请告诉我怎么做?

 public class VisibleColumnPanel extends Window implements Window.ResizeListener{

  public VisibleColumnPanel(){

    center();
    setResizable(true);
    setModal(true);
    setImmediate(true);
    //setSizeFull();
    addListener(this);
    this.setWidth(720, Unit.PIXELS);
    this.setHeight(600, Unit.PIXELS);
 }

@Override
public void windowResized(ResizeEvent resizeEvent) {

    System.out.println("hit here now");

}


}

普通窗口没有最大化: - enter image description here

最大化窗口后: - enter image description here

那么窗口内的组件是否也可以与窗口一起消耗或维持其配给量?

如果我可以处理最大化按钮点击事件,我可以以编程方式执行此操作。我实现了实现Window.ResizeListener。 windowResized事件仅在我尝试通过拖动鼠标来调整窗口大小时触发。有没有办法获得最大化/恢复按钮点击事件?

1 个答案:

答案 0 :(得分:1)

@AndréSchild的评论是正确的,例如将TwinColSelect设置为setWidth(“100%”)。

另请注意,有文档记录的错误,TwinColSelect调整大小并不总是在Window https://github.com/vaadin/framework/issues/10652中正确发生。好消息是修复它的工作正在进行中。

你不需要catch resize事件来完成你正在尝试的事情。相反,您应该正确使用布局参数,以便正确调整组件和布局。这样它可以更有效地工作(即没有服务器往返),因为浏览器将知道如何调整大小。

这是一个简化的示例应用

package com.example.myapplication;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Theme("mytheme")
public class MyUI extends UI {

    public class Person implements Serializable {

        public Person(String name, int birthyear) {
            this.name = name;
            this.birthyear = birthyear;
        }

        public String getName() {
            return name;
        }

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

        public int getBirthyear() {
            return birthyear;
        }

        public void setBirthyear(int birthyear) {
            this.birthyear = birthyear;
        }

        private String name;
        private int birthyear;
        }

    public class MyWindow extends Window implements Window.ResizeListener {
        final HorizontalLayout hLayout = new HorizontalLayout();

        public MyWindow() {
            setCaption("Grid");
            List<Person> people = Arrays.asList(
                new Person("Nicolaus Copernicus", 1543),
                new Person("Galileo Galilei", 1564),
                new Person("Johannes Kepler", 1571));

            Grid<Person> grid1 = new Grid<>();
            grid1.setItems(people);
            grid1.addColumn(Person::getName).setCaption("Name");
            grid1.addColumn(Person::getBirthyear).setCaption("Year of birth");
            grid1.setWidth("100%");

            VerticalLayout buttons = new VerticalLayout();

            Button button1 = new Button("<-");
            Button button2 = new Button("->");
            buttons.addComponents(button1,button2);
            buttons.setComponentAlignment(button1, Alignment.MIDDLE_CENTER);
            buttons.setComponentAlignment(button2, Alignment.MIDDLE_CENTER);

            Grid<Person> grid2 = new Grid<>();
            grid2.setItems(people);
            grid2.addColumn(Person::getName).setCaption("Name");
            grid2.addColumn(Person::getBirthyear).setCaption("Year of birth");
            grid2.setWidth("100%");

            hLayout.addComponents(grid1,buttons,grid2);
            hLayout.setExpandRatio(grid1, 3);
            hLayout.setExpandRatio(buttons, 1);
            hLayout.setExpandRatio(grid2, 3);
            hLayout.setWidth("100%");

            setContent(hLayout);
            setWidth("800px");
        }

        @Override
        public void windowResized(ResizeEvent e) {
            System.out.println("Window resized");
        }
    }

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        Label label = new Label("Window example");
        layout.addComponent(label);

        final MyWindow window = new MyWindow();

        addWindow(window);

        window.addResizeListener(event -> {
            Label lab = new Label("Window resized");
            layout.addComponent(lab);
        });

        setContent(layout);
    }

    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}