2013-05-17 76 views
0

我想通過單擊按鈕打印頁面,但以下代碼顯示運行時錯誤。我已經使用JFrame表單作爲打印頁面並添加了一個JButton和一個JTextField。找到符號時有錯誤(JButton和JTextField)。任何人都可以幫忙?如何通過在java中選擇打印機選項來使用按鈕打印頁面

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 
} 
+0

請張貼您的錯誤。與缺失符號相關的錯誤通常很容易找到並修復。如果你做了很多複製/粘貼,確保你使用的所有變量都被聲明瞭。 – rimero

回答

3

的錯誤是在函數內部

private void initComponents(){ ... } 

使得

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

等。

相關問題