gpt4 book ai didi

java - 无法将组件添加到布局

转载 作者:行者123 更新时间:2023-12-01 09:43:31 26 4
gpt4 key购买 nike

大家好,我遇到了一个奇怪的问题,但我确信我在做一些愚蠢的事情。在 Maven 项目中,我的 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 );
}
}

所以我的问题是,当我在 UI 课上时

layout.addComponents(title, calculation);

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

最佳答案

方法addComponents(...)仅接受Component作为参数。您的 Calculation 类未扩展/实现 Component

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

关于java - 无法将组件添加到布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38254413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com