0
我是新來的java和jasperreport。我很難執行一個小報告。我添加了所有必需的jar文件,並沒有遇到任何錯誤。ireport 3.7.4 netbeans 6.9.1將不會執行
如何使用netbeans自動生成一個jbutton的「mouseclicked」事件來生成pdf文件? 以下是我已經從thainetbeans和iREPORT分享到人人教程網站的教程中使用的代碼:
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.*;
import net.sf.jasperreports.view.*;
import net.sf.jasperreports.engine.xml.*;
public class MyiReportViewer extends javax.swing.JFrame {
/** Creates new form MyiReportViewer */
final String JDBC = "com.mysql.jdbc.Driver";
final String DB = "jdbc:mysql://localhost/afemdb?user=root&password";
private Connection con;
/** Creates new form NewJPanel */
public MyiReportViewer() {
initComponents();
try{
Class.forName(JDBC).newInstance();
con =DriverManager.getConnection(DB);
}
catch(Exception e){
}
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addComponent(jButton1)
.addContainerGap(230, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(jButton1)
.addContainerGap(205, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
try{
//JasperDesign jasperDesign = JRXmlLoader.load("C:\\Documents and Settings\\Lelou\\Desktop\\presentation\\InstructorQuestionaire.jrxml");
//JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
//JasperViewer.viewReport(jasperPrint, true);
JasperReport report = JasperCompileManager.compileReport("C:\\Documents and Settings\\Lelou\\Desktop\\presentation\\InstructorQuestionaire.jrxml");
JasperPrint print = JasperFillManager.fillReport(report, null, con);
JRViewer viewer = new JRViewer(print);
viewer.setOpaque(true);
viewer.setVisible(true);
}
catch(Exception e){
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// try{
// JasperDesign jasperDesign = JRXmlLoader.load("C:\\Documents and Settings\\Lelou\\Desktop\\presentation\\InstructorQuestionaire.jrxml");
// JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
// JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, con);
// JasperViewer.viewReport(jasperPrint, true);
//JasperReport report = JasperCompileManager.compileReport("C:\\Documents and Settings\\Lelou\\Desktop\\presentation\\InstructorQuestionaire.jrxml");
//JasperPrint print = JasperFillManager.fillReport(report,null, con);
//JRViewer viewer = new JRViewer(print);
//viewer.setOpaque(true);
//viewer.setVisible(true);
// }
// catch(Exception e){
//}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyiReportViewer().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
我是否需要導入一個軟件包...出現此錯誤符號:class URL URL reportFileURL = getClass()。getResource(「C:\\ Documents and Settings \\ Lelou \\ Desktop \\ presentation \\ InstructorQuestionaire.jrxml「);和符號:class URISyntaxException } catch(URISyntaxException exs){我的佈局上的道歉 – 2011-03-17 08:52:42
發生另一個錯誤...線程「AWT-EventQueue-0」中的異常java.lang.NullPointerException ...感謝Costis幫助了我輸出^^ – 2011-03-17 09:11:47
LOC上沒有錯誤...按下按鈕後出現...輸出錯誤大到在這裏發佈... T_T – 2011-03-17 09:33:52