如何将关闭按钮添加到GWT对话框的标题栏

如何将关闭按钮添加到GWT对话框的标题栏,gwt,Gwt,我需要在对话框的标题栏中添加一个关闭按钮。我可以在标题栏中放置小部件,但无法获取它的事件。 看起来正是您想要的。这里是对amal给出的示例的修改。此代码保留gwt对话框标题样式 public class CloseButtonDialogBox extends DialogBox { private Node closeEventTarget = null; public CloseButtonDialogBox() { // get the "dialogTopRight" cla

我需要在对话框的标题栏中添加一个关闭按钮。我可以在标题栏中放置小部件,但无法获取它的事件。


看起来正是您想要的。

这里是对amal给出的示例的修改。此代码保留gwt对话框标题样式

public class CloseButtonDialogBox extends DialogBox {

private Node closeEventTarget = null;

public CloseButtonDialogBox() {
    // get the "dialogTopRight" class td
    Element dialogTopRight = getCellElement(0, 2);

    // close button image html
    dialogTopRight.setInnerHTML(
            "<div style=\"margin-left:-25px;margin-top: 7px;\">" + 
            "<img src=\"images/closebutton.png\" height=\"20px\"/>" + 
            "</div>");

    // set the event target
    closeEventTarget = dialogTopRight.getChild(0).getChild(0);
}

@Override
protected void onPreviewNativeEvent(NativePreviewEvent event) {
    NativeEvent nativeEvent = event.getNativeEvent();

    if (!event.isCanceled() 
            && (event.getTypeInt() == Event.ONCLICK)
            && isCloseEvent(nativeEvent))
    {
        this.hide();
    }
    super.onPreviewNativeEvent(event);
}

// see if the click target is the close button
private boolean isCloseEvent(NativeEvent event) {
    return event.getEventTarget().equals(closeEventTarget); //compares equality of the underlying DOM elements
}   
公共类CloseButtonDialogBox扩展对话框{
私有节点closeEventTarget=null;
public CloseButtonDialogBox(){
//获取“dialogTopRight”类td
元素dialogTopRight=getCellElement(0,2);
//关闭按钮图像html
dialogTopRight.setInnerHTML(
"" + 
"" + 
"");
//设置事件目标
closeEventTarget=dialogTopRight.getChild(0).getChild(0);
}
@凌驾
受保护的void OnPreviewActiveEvent(NativePreviewEvent事件){
NativeEvent NativeEvent=event.getNativeEvent();
如果(!event.isCanceled()
&&(event.getTypeInt()==event.ONCLICK)
&&isCloseEvent(nativeEvent))
{
this.hide();
}
超级预览活动(事件);
}
//查看单击目标是否为关闭按钮
私有布尔值isCloseEvent(NativeEvent事件){
return event.getEventTarget().equals(closeEventTarget);//比较底层DOM元素的相等性
}   

查看链接问题中的答案。 它通过实现DialogBox.Caption接口,然后为隐藏对话框的包含关闭按钮向标题实现添加事件处理程序,展示了正确的方法

这对我起了作用


我最终编写了自己的对话框,从PopupPanel扩展而来。您还可以在样式中添加“光标:指针”,将鼠标更改为指针以获得效果;有关如何使按钮看起来“按下”的信息,请参见