⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printerapplet.java

📁 tomcat最新安装程序
💻 JAVA
字号:
import dori.jasper.engine.*;
import dori.jasper.engine.util.*;

import java.io.*;
import java.net.*;
import java.awt.BorderLayout;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;

/**
 *
 */
public class PrinterApplet extends javax.swing.JApplet{

    private URL url=null;
    private JasperPrint jasperPrint=null;

    /** Creates new form AppletViewer */
    public PrinterApplet(){
        initComponents();
    }

    /**
     *
     */
    public void init(){
        String strUrl=getParameter("REPORT_URL");
        if(strUrl!=null){
            try{
                url=new URL(getCodeBase(),strUrl);
            } catch(Exception e){
                StringWriter swriter=new StringWriter();
                PrintWriter pwriter=new PrintWriter(swriter);
                e.printStackTrace(pwriter);
                JOptionPane.showMessageDialog(this,swriter.toString());
            }
        } else{
            JOptionPane.showMessageDialog(this,"Source URL not specified");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents(){ //GEN-BEGIN:initComponents
        pnlMain=new javax.swing.JPanel(new FlowLayout()); 
        btnPrint=new javax.swing.JButton();
        btnView=new javax.swing.JButton();
       
		
		//pnlMain.setLayout(null);
        //pnlMain.setSize(200,40);
        btnPrint.setText("打印");
    	btnPrint.setForeground(Color.blue);
        btnPrint.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                btnPrintActionPerformed(evt);
            }
        });

        pnlMain.add(btnPrint);
        //btnView.setFont(new Font("预览",2,10));
        btnView.setText("预览");
        
		btnView.setForeground(Color.blue);
        btnView.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt){
                btnViewActionPerformed(evt);
            }
        });

        pnlMain.add(btnView);
        pnlMain.setBackground(new Color(171,224,203));
        //btnPrint.setBounds(120,0,50,35);
        //btnView.setBounds(30,0,50,35);
        getContentPane().add(pnlMain,java.awt.BorderLayout.WEST);

    } //GEN-END:initComponents

    private void btnViewActionPerformed(java.awt.event.ActionEvent evt){ //GEN-FIRST:event_btnViewActionPerformed
        // Add your handling code here:
        if(url!=null){
            try{
                if(jasperPrint==null){
                    jasperPrint=(JasperPrint)JRLoader.loadObject(url);
                }
                if(jasperPrint!=null){
                    ViewerFrame viewerFrame=new ViewerFrame(this.
                        getAppletContext(),jasperPrint);
                    viewerFrame.show();
                } else{
                    JOptionPane.showMessageDialog(this,"Empty report.");
                }
            } catch(Exception e){
                StringWriter swriter=new StringWriter();
                PrintWriter pwriter=new PrintWriter(swriter);
                e.printStackTrace(pwriter);
                JOptionPane.showMessageDialog(this,swriter.toString());
            }
        } else{
            JOptionPane.showMessageDialog(this,"Source URL not specified");
        }
    } //GEN-LAST:event_btnViewActionPerformed

    private void btnPrintActionPerformed(java.awt.event.ActionEvent evt){ //GEN-FIRST:event_btnPrintActionPerformed
        // Add your handling code here:
        if(url!=null){
            if(jasperPrint==null){
                try{
                    jasperPrint=(JasperPrint)JRLoader.loadObject(url);
                } catch(Exception e){
                    StringWriter swriter=new StringWriter();
                    PrintWriter pwriter=new PrintWriter(swriter);
                    e.printStackTrace(pwriter);
                    JOptionPane.showMessageDialog(this,swriter.toString());
                }
            }

            if(jasperPrint!=null){
                Thread thread=new Thread
                              (
                    new Runnable(){
                    public void run(){
                        try{
                            JasperPrintManager.printReport(jasperPrint,true);
                        } catch(Exception e){
                            StringWriter swriter=new StringWriter();
                            PrintWriter pwriter=new PrintWriter(swriter);
                            e.printStackTrace(pwriter);
                            JOptionPane.showMessageDialog(null,swriter.toString());
                        }
                    }
                }
                );

                thread.start();
            } else{
                JOptionPane.showMessageDialog(this,"Empty report.");
            }
        } else{
            JOptionPane.showMessageDialog(this,"Source URL not specified");
        }
    } //GEN-LAST:event_btnPrintActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel pnlMain;
    private javax.swing.JButton btnView;
    private javax.swing.JButton btnPrint;
    // End of variables declaration//GEN-END:variables

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -