ボタンをクリックしてページを印刷したいのですが、次のコードは実行時エラーを示します。JFrame フォームを印刷ページとして使用し、JButton と JTextField を追加しました。シンボル (JButton および JTextField) を見つける際にエラーが発生します。誰でも助けることができますか?
package yelloclient;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.print.*;
import java.awt.geom.*;
/**
*
* @author Pavan Poudel
*/
@SuppressWarnings("unchecked")
public class PrintDoc extends javax.swing.JFrame {
/**
* Creates new form PrintDoc
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PrintDoc().setVisible(true);
}
});
}
private void PrintDoc() {
initComponents();
}
private void initComponents() {
jButton1 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Print");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jTextField1.setText("Hello, how are you ?");
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(164, 164, 164)
.addComponent(jButton1)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(50, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(26, 26, 26)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 152, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(81, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
PrintDoc pr = new PrintDoc();
PrintForm.printComponent(pr);
}
}
class PrintForm implements Printable {
private Component component;
public static void printComponent(Component c) {
new yelloclient.txtPrint(c).print();
}
public PrintForm(Component component) {
this.component = component;
}
public void print() {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintable(this);
if (printJob.printDialog())
try {
printJob.print();
} catch(PrinterException pe) {
System.out.println("Error in printing !!! " + pe);
}
}
public int print(Graphics g, PageFormat pf, int pageIndex) {
RepaintManager currentManager = RepaintManager.currentManager(component);
if (pageIndex > 0)
{
return(NO_SUCH_PAGE);
} else {
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/** Disable double buffering as double buffering slows the printing*/
currentManager.setDoubleBufferingEnabled(false);
component.paint(g2d);
currentManager.setDoubleBufferingEnabled(true);
return(PAGE_EXISTS);
}
}
// Variables declaration - do not modify
public javax.swing.JButton jButton1;
public javax.swing.JTextField jTextField1;
// End of variables declaration
}