如何使用java中的打印机选项使用按钮打印页面

时间:2013-05-17 18:50:15

标签: java swing printing jbutton jtextfield

我想通过单击按钮打印页面,但以下代码显示运行时错误。我使用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
}

1 个答案:

答案 0 :(得分:3)

错误在函数

private void initComponents(){ ... } 

这样

cannot find symbol
symbol:   variable jButton1
location: class PrintDoc
----
(Alt-Enter shows hints)

等等。