如何在 netbeans 中单击按钮后从 java 程序打开创建的文件
How to open a created file from java program after button click in netbeans
我在 netbeans 中创建了一个程序,其中单击按钮会在主目录中创建一个 pdf 文件,但是我必须添加哪些代码才能在单击按钮后打开它。
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 我可以在我的 frograms 目录中创建 pdf 文件,我想在单击相同按钮创建时打开它
我会尝试 Desktop.open(File)
,其中:
启动关联的应用程序以打开文件。
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
你可以在你的 actionperfomrmed-method 中使用这样的东西:File yourSavedFile = new File("path to your saved file")
。之后,您可以从该文件中读取并显示它。
我在 netbeans 中创建了一个程序,其中单击按钮会在主目录中创建一个 pdf 文件,但是我必须添加哪些代码才能在单击按钮后打开它。
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 我可以在我的 frograms 目录中创建 pdf 文件,我想在单击相同按钮创建时打开它
我会尝试 Desktop.open(File)
,其中:
启动关联的应用程序以打开文件。
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
你可以在你的 actionperfomrmed-method 中使用这样的东西:File yourSavedFile = new File("path to your saved file")
。之后,您可以从该文件中读取并显示它。