GWT PopupPanel只出现一次

时间:2013-10-03 17:18:10

标签: gwt popuppanel

我正在使用带有以下代码的GWT-Popup-Panel:

私有静态类MyPopup扩展了PopupPanel {

        public MyPopup() {
          // PopupPanel's constructor takes 'auto-hide' as its boolean parameter.
          // If this is set, the panel closes itself automatically when the user
          // clicks outside of it.
          super(true);

          // PopupPanel is a SimplePanel, so you have to set it's widget property to
          // whatever you want its contents to be.
          setWidget(new Label("Click outside of this popup to close it"));

        }
      }



public void onModuleLoad() {

     final Button b1 = new Button("About");
        b1.addClickHandler(new ClickHandler() {
          public void onClick(ClickEvent event) {
            final MyPopup g = new MyPopup();
            g.setWidget(RootPanel.get("rightagekeyPanel"));
            g.setPopupPositionAndShow(new PopupPanel.PositionCallback() {
                public void setPosition(int offsetWidth, int offsetHeight) {
                  g.setPopupPosition(b1.getAbsoluteLeft(), b1.getAbsoluteTop());
                  g.setAutoHideEnabled(true);
                }
              });

            g.setVisible(true);
            g.setWidth("500px");
            g.setHeight("500px");

            g.show();

          }
        });

单击按钮b1时会出现,但第二次单击时不会出现。怎么了?

2 个答案:

答案 0 :(得分:1)

ClickHandler之外设置一个与Button处于同一级别的弹出窗口。您也不需要PositionCallback来居中弹出窗口。您只需致电g.center()即可显示并居中。 GWT支持页面上的一个已知问题是,如果您没有为其设置宽度,它将无法正确居中。如果你给弹出一个合适的宽度,它将正确居中。

它不再显示的原因是因为您删除了RootPanel.get("rightagekeyPanel")中的小部件并将其放入弹出窗口。下次您尝试这样做时不再存在。

窗口小部件一次只能放在一个位置,因此如果将其从父窗口中删除,请使用变量或其他内容跟踪它,以便重新使用它。否则,您必须重新实例化小部件。

public void onModuleLoad() {

    final Button b1 = new Button("About");
    final MyPopup g = new MyPopup(); //create only one instance and reuse it.
    g.setAutoHideEnabled(true);
    g.setSize("500px", "500px"); //sets width AND height


    b1.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {

            g.setWidget(RootPanel.get("rightagekeyPanel"));//DON'T DO THIS.

            g.center();//will show it and center it.
        }
    });
}

答案 1 :(得分:1)

就我的情况说,我必须添加一些小部件才能显示PopUpPanel。尝试使用标签来确保Popup正在显示。

    PopupPanel popup = new PopupPanel();    
    popup.setVisible(true);
    popup.center();
    popup.show();
    popup.setWidth("500px");
    popup.setHeight("500px");
    popup.add(new Label("Test"));