2010-07-06 136 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() 
    } 
}