8

ブラウザウィンドウのサイズ変更時にポップアップのサイズを変更する必要があります。ポップアップ コンストラクターに ResizeHandler を追加しましたが、いくつかのブラウザーのサイズ変更 center() 関数の後、現在のポップアップを中央に配置する代わりに、新しいポップアップを作成します。ここで、私がすでに試したコードをいくつか示します。これを解決する方法を教えてください。または、いくつかの解決策を提案してください。

public BigPopup() {
...
    final BigPopup self = this;
        Window.addResizeHandler(new ResizeHandler() {           
            @Override
            public void onResize(ResizeEvent event) {
                self.setHeight(getNewHeight());
                self.setWidth(getNewWidth());
                self.center();
            }
        });     
...
}

public BigPopup() {
...
        Window.addResizeHandler(new ResizeHandler() {           
            @Override
            public void onResize(ResizeEvent event) {
                BigPopup.this.setHeight(getNewHeight());
                BigPopup.this.setWidth(getNewWidth());
                BigPopup.this.center();
            }
        });     
...
}

追加した:

問題を説明する簡単なプロジェクトを作成しました: Class of Popup

package tesr.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;

public class BigPopup extends PopupPanel {

    private static BigPopupUiBinder uiBinder = GWT
            .create(BigPopupUiBinder.class);

    interface BigPopupUiBinder extends UiBinder<Widget, BigPopup> {
    }

    @UiField
    Button tstBtn;

    @UiHandler("tstBtn")
    void click(ClickEvent event) {
        this.hide();
    }

    public int[] getSize() {
        int[] mas = new int[2];
        int x = Window.getClientWidth();

        int y = Window.getClientHeight();

        if (x >= 1024) {
            mas[0] = x - 100;
            mas[1] = y - 100;
        } else {
            mas[0] = 1024;
            mas[1] = 768;
        }

        return mas;
    }

    public BigPopup() {
        setWidget(uiBinder.createAndBindUi(this));
        Window.addResizeHandler(new ResizeHandler() {
            @Override
            public void onResize(ResizeEvent event) {
                BigPopup.this.setHeight(getSize()[1] + "px");
                BigPopup.this.setWidth(getSize()[0] + "px");
                BigPopup.this.center();
            }
        });
        this.setHeight(getSize()[1] + "px");
        this.setWidth(getSize()[0] + "px");
        this.setAnimationEnabled(true);
        this.setGlassEnabled(true);
        this.setAutoHideEnabled(false);
    }

}

uibinder の XML

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <ui:style>

    </ui:style>
    <g:HTMLPanel>
        <g:Label text="testLabel"></g:Label>
        <g:Button text="testButton" ui:field="tstBtn"></g:Button>
    </g:HTMLPanel>
</ui:UiBinder> 

そしてメインクラス

package tesr.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;

public class AAA implements EntryPoint {

    public void onModuleLoad() {    
        Button btn = new Button("Show Popup");
        btn.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {
                BigPopup popup = new BigPopup();
                popup.center();             
            }
        });
        RootPanel.get().add(btn);

    }
}

私は何を間違えていますか?それともGWTのバグでしょうか?

4

1 に答える 1

7

この問題は、おそらくサイズ変更とはまったく関係ありません。それはちょうどこれです:

  • [ポップアップを表示] ボタンをクリックすると、最初の PopupPanel が作成されます。
  • この Panel では、ウィンドウに ResizeHandler を登録します。
  • 「testButton」をクリックすると、ポップアップが非表示になりますが、ResizeHandler の登録は解除されません。
  • 次に、[ポップアップの表示] をもう一度クリックすると、別の ResizeHandler を使用して別のポップアップが作成されます。

したがって、2 つのポップアップと 2 つの ResizeHandlers があります。

ここで、サイズ変更時に「center()」を呼び出します (これは両方の ResizeHandlers で発生します)。これには、ポップアップが表示されるという副作用があります (現在表示されていない場合)。最初のポップアップは現在 DOM から切り離されていますが、GWT はそれを再付加するのに十分スマートです。しかし、今では両方を同時に見ることができます。

解決策は、ポップアップを非表示にするときにサイズ変更ハンドラーを削除することです。

方法がないと思うかもしれませんcom.google.gwt.user.client.Window.removeResizeHandler()。動作が少し異なります。

private final HandlerRegistration handlerRegistration;

public BigPopup() {
  setWidget(uiBinder.createAndBindUi(this));
  handlerRegistration = Window.addResizeHandler(new ResizeHandler() {...});
  ...
}
@UiHandler("tstBtn")
void click(final ClickEvent event) {
   handlerRegistration.removeHandler();
   this.hide();
}

また、ポップアップが表示されている間は、[ポップアップを表示] ボタンを無効にする必要があります。

于 2012-05-11T18:30:35.537 に答える