0

我正在使用面板和组件打印各种元素,例如文本和图像,它正在生成用于打印的页面,打印也即将到来,但硬打印在物理纸上也有打印按钮。我从页面上删除打印按钮。这是代码

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

2 回答 2

0

你可以

  1. 为打印图形定义自定义剪辑以排除打印按钮区域

或者 2. 只是隐藏按钮

或 3. 覆盖按钮的 paintComponent() 方法,其中检查 isPrint 标志(如果它是 false,则调用 super,它是 true,什么都不做)。

于 2013-09-26T14:15:21.017 回答
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);

反而...

于 2013-09-27T07:34:32.267 回答