2013-09-26 42 views
0

我正在使用正在生成打印頁面的面板和組件打印文本和圖像等各種元素,並且打印也即將到來,但硬打印也在物理紙張上具有打印按鈕。我從頁面中刪除打印按鈕。這裏是代碼在java中的實際硬拷貝上發生打印按鈕

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.print.*; 

public class Printing extends JFrame 
          implements ActionListener { 
    public static void main(String[] args) 
    { 
    // intialise 

    } 

    public Printing(String Firstname,String LastName,String contactid) 
    { 
    super("Print badge"); 
    WindowUtilities.setNativeLookAndFeel(); 
    addWindowListener(new ExitListener()); 
    Container content = getContentPane(); 
    JButton printButton = new JButton("Print"); 
    printButton.addActionListener(this); 
    JPanel buttonPanel = new JPanel(); 
    buttonPanel.setBackground(Color.BLACK); 
    buttonPanel.add(printButton); 
    content.add(buttonPanel, BorderLayout.SOUTH); 
    DrawingPanel drawingPanel = new DrawingPanel(Firstname,LastName,contactid); 
    content.add(drawingPanel, BorderLayout.CENTER); 
    pack(); 
    setVisible(true); 
    } 

    public void actionPerformed(ActionEvent event) 
    { 
     //call for printing 


    } 
} 

和代碼用於實際打印的面板

public class DrawingPanel extends JPanel 
{ 
    private int fontSize1 = 32; 

    private Image img1=null; 


    public DrawingPanel(String n1,String n2,String n3) 
    { 

    String path="D:"+"\\25175.jpg"; 



    setBackground(Color.white); 
    Font font = new Font("Serif", Font.PLAIN, 32); 
    setFont(font); 

    img1=new ImageIcon(path).getImage(); 


    setPreferredSize(new Dimension(400, 400)); 
    } 

    public void paintComponent(Graphics g) 
    { 

    Graphics2D g2d = (Graphics2D)g; 

    g2d.translate(x, y); 
    g2d.setPaint(Color.lightGray); 
    AffineTransform origTransform = g2d.getTransform(); 
    g2d.shear(-0.95, 0); 
    g2d.scale(1, 3); 

    g2d.setTransform(origTransform); 
    g2d.setPaint(Color.BLUE); 
    g2d.drawString(string,25 , 50); 
    g2d.drawString(string, 125,100); 
    g.drawImage(img1, 280, 190, null); 
    } 
} 

印刷方法設置在這裏

import java.awt.*; 
import javax.swing.*; 
import java.awt.print.*; 

public class PrintUtilities implements Printable { 
    private Component componentToBePrinted; 

    public static void printComponent(Component c) { 
    new PrintUtilities(c).print(); 
    } 

    public PrintUtilities(Component componentToBePrinted) { 
    this.componentToBePrinted = componentToBePrinted; 
    } 



    public void print() { 
    PrinterJob printJob = PrinterJob.getPrinterJob(); 
    printJob.setPrintable(this); 
    if (printJob.printDialog()) 
     try { 
     printJob.print(); 
     } catch(PrinterException e) { 
     System.out.println("Error printing: " + e); 
     } 
    } 

    public int print(Graphics g, PageFormat pageFormat, int pageIndex) { 
    if (pageIndex > 0) { 
     return(NO_SUCH_PAGE); 
    } else { 
     Graphics2D g2d = (Graphics2D)g; 
     g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); 

     disableDoubleBuffering(componentToBePrinted); 
     componentToBePrinted.paint(g2d); 
     enableDoubleBuffering(componentToBePrinted); 
     return(PAGE_EXISTS); 
    } 
    } 

    public static void disableDoubleBuffering(Component c) { 
    RepaintManager currentManager = RepaintManager.currentManager(c); 
    currentManager.setDoubleBufferingEnabled(false); 
    } 

    public static void enableDoubleBuffering(Component c) { 
    RepaintManager currentManager = RepaintManager.currentManager(c); 
    currentManager.setDoubleBufferingEnabled(true); 
    } 
} 
+0

你似乎已經離開了所有可能向我們展示的重要印刷的東西,爲什麼你似乎得到CON帳篷窗格的框架打印,而不僅僅是我們的繪圖面板... – MadProgrammer

+0

我已經添加了打印設置的代碼和打印方法 – MandarCT

+0

那麼您實際發送給這個類的組件是什麼? – MadProgrammer

回答

0

在按鈕的動作監聽此調用PrintUtilities.printComponent(本)

這似乎是你的問題。

這將在技術上打印整個框架的內容,您只需要打印DrawingPanel

這將需要你,讓你從你的actionPerformed方法訪問DrawingPanel的實例,使你的代碼一些細微的變化......

private DrawingPanel drawingPanel; 
public Printing(String Firstname,String LastName,String contactid) 
{ 
    super("Print badge"); 
    //... 
    drawingPanel = new DrawingPanel(Firstname,LastName,contactid); 
    //... 
} 

這不應該讓你做這樣的事情。 ..

PrintUtilities.printComponent(drawingPanel); 

,而不是...

+0

它現在正在工作。 – MandarCT

0

可以

  1. 定義自定義剪輯爲打印圖形以排除打印按鈕區域

或2只隱藏按鈕

或3.覆蓋按鈕的的paintComponent()方法,其中檢查isprint判斷標誌(如果它是假的通話超真的什麼都不做)。

+0

給出一些小代碼啓動 – MandarCT