gpt4 book ai didi

java - 我的 Printable Swing 组件的所有部分都无法打印

转载 作者:太空宇宙 更新时间:2023-11-04 08:53:24 28 4
gpt4 key购买 nike

我正在尝试制作一个可打印组件(发票文档)。我使用 JComponent 而不是 JPanel 因为我不需要背景。该组件有许多子组件。

主组件实现 Printable 并具有一个 print 方法,该方法调用 printAll(g) 以便打印所有子组件。但我的子组件没有打印。

我错过了什么?所有子组件都必须实现 Printable 吗?

在下面的代码中,未打印 TopHeader

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class PPanel extends JComponent implements Printable {
static double w;
static double h;

public PPanel() {
this.setLayout(new BorderLayout());

this.add(new JLabel("Document Body"), BorderLayout.CENTER);
this.add(new TopHeader(), BorderLayout.NORTH);
}

class TopHeader extends JComponent {
public TopHeader() {
this.setLayout(new BorderLayout());
JLabel companyName = new JLabel("Company name");
JLabel docType = new JLabel("Document type");
this.add(companyName, BorderLayout.WEST);
this.add(docType, BorderLayout.EAST);
}
}

public static void main(String[] args) {
final PPanel p = new PPanel();
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(p);
try {
job.print();
} catch (PrinterException ex) {
// print failed
}
// Preview
new JFrame() {{ getContentPane().add(p); this.setSize((int)w, (int)h); setVisible(true); }};

}

@Override
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}

Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());

w = pf.getImageableWidth();
h = pf.getHeight();

this.setSize((int)w, (int)h);
this.setPreferredSize(new Dimension((int)w, (int)h));
this.doLayout();

this.printAll(g);
return PAGE_EXISTS;
}
}

最佳答案

您可能可以循环遍历 getComponents() 返回的 Component[],但下面显示了一个简单的权宜之计。请注意屏幕预览和打印输出之间的差异。另外,请注意 validate() 的使用优先于 doLayout()。最后,为了方便预览,本示例实现了 Using Print Setup Dialogs .

附录:您还可以查看Printing Support in Swing Components并比较此 example 中采取的方法.

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

public class PPanel extends JComponent implements Printable {

private JComponent top = new TopHeader();
private JComponent mid = new JLabel("Document Body");

public PPanel() {
this.setLayout(new BorderLayout());
this.add(top, BorderLayout.NORTH);
this.add(mid, BorderLayout.CENTER);
}

private static class TopHeader extends JComponent {

public TopHeader() {
this.setLayout(new BorderLayout());
JLabel companyName = new JLabel("Company name");
JLabel docType = new JLabel("Document type");
this.add(companyName, BorderLayout.WEST);
this.add(docType, BorderLayout.EAST);
}
}

@Override
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
int w = (int)pf.getImageableWidth();
int h = (int)pf.getImageableHeight();
top.setSize(new Dimension(w, top.getPreferredSize().height));
this.setSize(w, h);
this.validate();
this.printAll(g2d);
return PAGE_EXISTS;
}

public static void main(String[] args) {
final PPanel p = new PPanel();
// Preview before print()
new JFrame() {
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(p);
this.pack();
this.setVisible(true);
}
};
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.pageDialog(pj.defaultPage());
pj.setPrintable(p, pf);
if (pj.printDialog()) {
try {
pj.print();
} catch (PrinterException pe) {
pe.printStackTrace();
}
}
}
}

关于java - 我的 Printable Swing 组件的所有部分都无法打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2865009/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com