我有两个带有两个窗口的按钮。按钮应按其各自的窗口显示内容。我想要按钮显示按钮一的内容和第二个按钮显示按钮一的内容。如何将按钮单击方法绑定到单击侦听器?
- - - - - - - - - - - - - - 编辑 - - - - - - - -
在建议的编辑后,按钮二显示“这是按钮 2”,但按钮一仅显示一个窗口,并且“这是按钮 1 永远不会出现”。
@Theme("mytheme")
public class MyUI extends UI implements Button.ClickListener {
final Button button = new Button("Click Me");
final Button button2 = new Button("Click Me");
final FormLayout content = new FormLayout();
final VerticalLayout layout = new VerticalLayout();
@Override
protected void init(VaadinRequest vaadinRequest) {
final Window window = new Window("Window");
final Window window2 = new Window("Window");
window.setContent(content);
window2.setContent(content);
button.addClickListener( e -> {
content.addComponent(new Label("This is Button 1"));
UI.getCurrent().addWindow(window);
});
button2.addClickListener( f -> {
content.addComponent(new Label("This is Button 2"));
UI.getCurrent().addWindow(window2);
});
layout.addComponents(button, button2);
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 {
}
}