GWT:打印按钮

时间:2010-07-06 09:56:55

标签: gwt jsni

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

这是我当前的代码,它使用(或至少尝试使用)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应用程序,所以我不确定如何实现它。

3 个答案:

答案 0 :(得分: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放在本机方法中。

答案 1 :(得分:2)

自GWT 1.5版以来,有一个内置的打印功能:

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

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

答案 2 :(得分: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() )