我需要在浏览器窗口调整大小时调整弹出窗口。我在弹出构造函数中添加了 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();
}
});
...
}
添加:
我创建了一个简单的项目来说明问题:弹出窗口类
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 的错误?