【问题标题】:GWT PopupPanel just appearing onceGWT PopupPanel 只出现一次
【发布时间】:2013-10-03 17:18:09
【问题描述】:

我正在使用带有以下代码的 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 时会出现,但第二次单击时不会出现。怎么了?

【问题讨论】:

  • 当你已经在构造函数中调用 setWidget() 时,为什么还要在 onClick() 中调用它?

标签: gwt popuppanel


【解决方案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.
        }
    });
}

【讨论】:

    【解决方案2】:

    就我而言,我必须添加一些小部件才能使 PopUpPanel 出现。尝试使用标签来确保弹出窗口正在显示。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多