java 打印机打印JTable 出现错误

图灵DUCK 2013-11-20 07:59:15
package frame;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.print.PageFormat;
import java.util.Properties;

import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.*;

public class Print {
public JTable table;
public DocAttributeSet attr;
public PrintRequestAttributeSet attrs ;
public PrintService[] services;
public PrintService service;
public PrintService selection;
public DocPrintJob job;
public DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN;//打印文本类型
public Doc doc;
public Print(JTable table){
this.table=table;
attr= new HashDocAttributeSet();
attrs = new HashPrintRequestAttributeSet();
services = PrintServiceLookup.lookupPrintServices(null, attr);
service = PrintServiceLookup.lookupDefaultPrintService();
service = PrintServiceLookup.lookupDefaultPrintService();
selection = ServiceUI.printDialog( null, 100, 100, services, service, null, attrs);
job = selection.createPrintJob();
doc = new SimpleDoc(table, flavor, attr);//构造具有指定打印数据、doc flavor 和 doc 属性集的 SimpleDoc

for (int i = 0; i < services.length; i++)
{
System.out.println(services[i].getName());
}
try {
job.print(doc, attrs);
} catch (PrintException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


这是自动报出的错==
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: data is not of declared type
at javax.print.SimpleDoc.<init>(Unknown Source)
at frame.Print.<init>(Print.java:37)
at frame.MainJFrame$1.mousePressed(MainJFrame.java:412)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

...全文
95 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
图灵DUCK 2013-11-20
  • 打赏
  • 举报
回复
==table.print();........

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧