我在netbeans中創建了一個程序,點擊按鈕在主目錄中創建一個pdf文件,但是我必須添加什麼代碼才能在點擊按鈕後打開它。如何在按鈕點擊netbeans後從java程序創建的文件
package demopdf;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author sunil
*/
public class DemoPdfCreate extends javax.swing.JFrame {
public static final String RESULT="Report.pdf";
/**
* Creates new form DemoPdfCreate
*/
public DemoPdfCreate() {
initComponents();
}
/**
* 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() {
jButton_pdf = new javax.swing.JButton();
jTextField_txt = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton_pdf.setText("Save To Pdf");
jButton_pdf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_pdfActionPerformed(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()
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(53, 53, 53)
.addComponent(jTextField_txt,
javax.swing.GroupLayout.PREFERRED_SIZE, 266,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(113, 113, 113)
.addComponent(jButton_pdf,
javax.swing.GroupLayout.PREFERRED_SIZE, 144,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(81, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTextField_txt,
javax.swing.GroupLayout.PREFERRED_SIZE, 159,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap
(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addComponent(jButton_pdf,
javax.swing.GroupLayout.PREFERRED_SIZE, 37,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(64, 64, 64))
);
pack();
}// </editor-fold>
private void jButton_pdfActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
Document doc =new Document();
try {
try {
PdfWriter.getInstance(doc, new FileOutputStream(RESULT));
} catch (FileNotFoundException ex) {
Logger.getLogger(DemoPdfCreate.class.getName()).log(Level.SEVERE, null,
ex);
}
doc.open();
doc.add(new Paragraph(jTextField_txt.getText()));
doc.close();
} catch (DocumentException ex) {
Logger.getLogger(DemoPdfCreate.class.getName()).log(Level.SEVERE, null,
ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting
code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the
default look and feel.
* For details see
http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info :
javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName
()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName
()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName
()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DemoPdfCreate.class.getName
()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> {
new DemoPdfCreate().setVisible(true);
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton_pdf;
private javax.swing.JTextField jTextField_txt;
// End of variables declaration
}
我上面的程序,並從創建,使用itextpdf.jar我可以能夠創建PDF文件在我frograms目錄時,它在相同的按鈕創建點擊打開這iwant
我不想使用openfiledialog,就像Process.Start和文件路徑在vb中使用一樣。應該在java中使用的代碼打開文件 –