应用程序类组织

时间:2016-07-23 13:28:36

标签: java vaadin vaadin7

我正在开发一个简单的应用程序,当单击一个按钮时,会打开另一个窗口作为弹出窗口:单击该按钮后,它将消失,只留下屏幕上的第二个窗口。当这个新窗口关闭时,原始按钮应该重新出现,但我有点不确定什么是最好的前进方式。我尝试用不同的类设计应用程序:一个只包含按钮的MyUi类,一个处理新窗口的NewWindow类和一个基本上具有进入新窗口的信息的Component类。我采用这种方法的问题是能够隐藏和显示原始按钮。我们来看一些代码。

MyUi.java has the button:
public class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        final NewWindow newWindow = new NewWindow();
       final UploaderComponent uploaderComponent = new UploaderComponent();
        //final TextField name = new TextField();
       // name.setCaption("Type your name here:");

      final Button button = new Button("Click Me");
        button.addClickListener(new Button.ClickListener()
        {
            @Override
            public void buttonClick(ClickEvent event) {
                //button.setVisible(false);
                // TODO Auto-generated method stub
                getUI().addWindow(newWindow.getWindow());
                newWindow.getWindow().setContent(uploaderComponent.formLayout);             
            }
        });


        layout.addComponents(button);
        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 {
    }
    public void resetButton(){
        //button.setVisible(true);
    }

这是NewWindow.java而不是

public class NewWindow {
    //public static final Sizeable.Unit PIXELS;
        Window window = new Window();
        public NewWindow(){
            window.setStyleName("wrappingWindow");
            window.setWidth("620px");
            window.center();
            window.addCloseListener(new CloseListener()
            {               
                @Override
                public void windowClose(CloseEvent e) {
                    System.out.println("window closed");

                }
            });

        }
        public Window getWindow(){
            return window;
        }
}

和组件类:

public class UploaderComponent extends CustomComponent {
    final TextField name;
    final TextField surname;
    //final Label div;
    final VerticalLayout formLayout = new VerticalLayout();

    public UploaderComponent(){

        formLayout.addStyleName("myLayout");



        name = new TextField();
        surname = new TextField();
        name.setCaption("Type your name here:");
        surname.setCaption("Type your surname here:");
        formLayout.addComponents(name, surname);

    }
}

通过这个设置,当button.setVisible(false);很容易点击按钮时,我可以让按钮消失但是我发现很难将它取回,因为我必须从窗口调用一个函数调用关闭按钮时的类,此函数必须访问位于UI类中的按钮...位杂乱。这是最好的方式吗?

0 个答案:

没有答案