2010-07-06 133 views
1

我正在尝试创建一个打印当前浏览器窗口的按钮。GWT:打印按钮

这是我当前的代码,使用(或者至少尝试使用)JSNI:

private Button print = new Button(constants.print(), new ClickHandler() { 
    @Override 
    public void onClick(final ClickEvent event) { 
     /*-{ 
      if ($wnd.print) { 
       $wnd.print(); 
       return true; 
      } else { 
       return false; 
      } 
     }-*/ 
    }   
}); 

但是,当我按一下按钮,没有任何反应。这是我的第一个GWT应用程序,所以我不确定如何实现它。

回答

6
new Button(constants.print(), new ClickHandler() { 
     @Override 
     public void onClick(final ClickEvent event) { 
      print(); 
     } 

     private native boolean print() /*-{ 
      if ($wnd.print) { 
       $wnd.print(); 
       return true; 
      } else { 
       return false; 
      } 
     }-*/; }); 

应该工作!始终将JSNI放在本地方法中。

+0

谢谢ve很多。有用。 – 2010-07-06 13:50:50

+8

顺便说一下,我发现你也可以使用由GWT提供的静态方法Window.print(),并避免使用JSNI。 – 2010-07-06 13:55:33

0

这里是我的2美分:

创建一个可重复使用的类:

public class PrintHandler implements ClickHandler { 

public void onClick (ClickEvent event) { 
    print(); 
} 

private native boolean print() 
/*-{ 
    if ($wnd.print) { 
     $wnd.print(); 
     return true; 
    } else { 
     return false; 
    } 
}-*/; 
} 

而且使用它您想要的位置:

new Button(constants.print(), new PrintHandler()) 
2

由于GWT 1.5版,有一个建-in打印功能:

import com.google.gwt.user.client.Window 

public class PrintHandler implements ClickHandler { 
    public void onClick (ClickEvent event) { 
      Window.print() 
    } 
}