无法将组件添加到布局中

时间:2016-07-07 20:09:52

标签: java layout vaadin vaadin7

大家好我有一个奇怪的问题,但我确定我做的事情很愚蠢。 在maven项目中,我有我的UI类,如下所示:

Caller       Date Duration   Status   Week
1    304 2016-02-01      756 ANSWERED Week 1
2    304 2016-02-01       61 ANSWERED Week 1
3    304 2016-02-04       60 ANSWERED Week 1
4    304 2016-02-10       61 ANSWERED Week 2
5    304 2016-02-17       60 ANSWERED Week 3
6    304 2016-02-19       30 ANSWERED Week 3
7    304 2016-02-24       27 ANSWERED Week 4
8    304 2016-02-28       55 ANSWERED Week 4
9    304 2016-02-28       63 ANSWERED Week 4

然后我有另一个包含各种字段的类,然后我尝试将其添加到UI中,但未成功:

package my.vaadin.project.exceptionTest;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
@Theme("mytheme")
@Widgetset("my.vaadin.project.exceptionTest.MyAppWidgetset")
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        final Calculation calculation = new Calculation();
        final Label title = new Label("Calculation");       
        layout.addComponents(title, calculation);       
        layout.setMargin(true);
        layout.setSpacing(true);        
        setContent(layout);
    }
    @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    public static class MyUIServlet extends VaadinServlet {
    }
}

所以我的问题是,在UI课程中我做

package my.vaadin.project.exceptionTest;
import java.awt.Component;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.event.Action;
public class Calculation extends CustomComponent{
    final VerticalLayout vl = new VerticalLayout();
    final TextField divisor = new TextField();
    final TextField dividend = new TextField();
    Button button = new Button("Click Me"); 
    public Calculation(){
        divisor.setCaption("Enter the divisor:");
        dividend.setCaption("Enter the dividend:");
        button.addClickListener( new Button.ClickListener(){
            @Override
            public void buttonClick(ClickEvent event) {
                System.out.println("this is a test");               
            }            
        });     
        vl.setMargin(true);
        vl.setSpacing(true);
        vl.addComponents(divisor, dividend, button );
    }
}

我收到一条错误消息:“AbstractComponentContainer类型中的方法addComponents(Component ...)不适用于参数(Label,Calculation)” 我之前做过类似的事情,比如创建一个单独的类的对象并将其添加到布局中它已经有效,这次它没有,我不确定我做错了什么.. 。任何的想法? 感谢

1 个答案:

答案 0 :(得分:1)

方法addComponents(...)仅接受Component作为参数。 您的Calculation课程未延伸/实施Component

https://vaadin.com/api/com/vaadin/ui/AbstractComponentContainer.html#addComponents(com.vaadin.ui.Component...)