将JasperReport jrxml加载到JFrame

时间:2011-11-13 11:27:35

标签: java oracle swing jasper-reports

如何查看JFrame内的jrxml文件?

我有以下java代码:

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;


public class Test extends JFrame {

    private Connection conn = null; // database connection

    public Test() {

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:XE",
                    "test", "test");
            JasperReport report = JasperCompileManager.compileReport("report1.jrxml");
            JasperPrint print = JasperFillManager.fillReport(report, null, conn);
            JRViewer viewer = new JRViewer(print);
            viewer.setOpaque(true);
            viewer.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
            }
        }

        setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Test();
    }
}

但是,当我尝试运行它时,收到以下错误消息:

log4j:WARN No appenders could be found for logger(net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).

log4j:WARN Please initialize the log4j system properly.

我的JFrame出现但是没有我要嵌入的报告。

您认为我的错误是什么?

1 个答案:

答案 0 :(得分:3)

JRViewerJPanel,因此不是..

viewer.setVisible(true);

放......

// Add the viewer to the frame
add(viewer);

暂且不说:

} catch (Exception e) {
}

将其更改为..

} catch (Exception e) {
    e.printStackTrace();
}