public class MyDialogBox extends DialogBox
{
private Label closeBtn;
private Label captiontLabel;
public MyDialogBox()
{
super();
this.createCaptionWithClose();
this.setModal(true);
this.setAutoHideOnHistoryEventsEnabled(true);
this.setAnimationEnabled(true);
this.setGlassEnabled(true);
}
private void createCaptionWithClose()
{
closeBtn = new Label("×");
closeBtn.setStylePrimaryName("close");
FlexTable captionLayoutTable = new FlexTable();
captionLayoutTable.setWidth("100%");
captiontLabel = new Label("");
captionLayoutTable.setWidget(0, 0, captiontLabel);
captionLayoutTable.setWidget(0, 1, closeBtn);
captionLayoutTable.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.HorizontalAlignmentConstant.endOf(HasDirection.Direction.LTR));
HTML caption = (HTML) getCaption();
caption.getElement().appendChild(captionLayoutTable.getElement());
caption.addClickHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
EventTarget target = event.getNativeEvent().getEventTarget();
Element targetElement = (Element) target.cast();
if (targetElement == closeBtn.getElement())
{
closeBtn.fireEvent(event);
}
}
});
addCloseHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
hide();
}
});
}
public void addCloseHandler(ClickHandler handler)
{
closeBtn.addClickHandler(handler);
}
}原文:http://blog.csdn.net/lizeyang/article/details/18421753