在java中的实际硬拷贝中出现打印按钮

时间:2013-09-26 11:57:55

标签: java swing button printing

我正在使用面板和组件打印各种元素,如文本和图像,它正在生成用于打印和打印的页面,但是硬打印也在实际纸张上有打印按钮。我要从页面中删除打印按钮。这是代码

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);
  }
}

2 个答案:

答案 0 :(得分:0)

你可以

  1. 为打印图形定义自定义剪辑以排除打印按钮区域
  2. 或2.只需隐藏按钮

    OR 3.覆盖按钮的paintComponent()方法,其中检查isPrint标志(如果是false,则调用super,它不执行任何操作)。

答案 1 :(得分:0)

  

通过调用按钮动作侦听器中的PrintUtilities.printComponent(this)

这似乎是你的问题。

从技术上讲,这将打印整个画面的内容,您只需要打印DrawingPanel

这将要求您对代码进行一些细微更改,以便从DrawingPanel方法访问actionPerformed的实例...

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

这不应该允许你做像...这样的事情。

PrintUtilities.printComponent(drawingPanel);

,而不是...