两种方法:
1.自己写打印类,可参考
http://www-900.ibm.com/developerWorks/cn/java/l-javaprint/index.shtml
http://www-900.ibm.com/developerWorks/cn/java/j-merlin/part10/index.shtml
2.使用itext等第三方jar实现
1.自己写打印类,可参考
http://www-900.ibm.com/developerWorks/cn/java/l-javaprint/index.shtml
http://www-900.ibm.com/developerWorks/cn/java/j-merlin/part10/index.shtml
2.使用itext等第三方jar实现
解决方案 »
- parse java Struct to oracle
- java.lang.NoSuchMethodError 达人们出手相救阿!
- 小妹初学求救!!Iterator具体含义是什么啊!
- 在jcreator中怎么建窗体和控件??
- 内存问题: 如何检查一个实例占用了多少内容?
- JAVA中的输入整数用什么函数?
- (1)socket能不能在applet中运用?(2)应用程序中能不能播放声音?谢谢!
- 谁有JReport的KEY,谢谢?请发至[email protected]
- 在哪儿下java的教程
- 请教如何设置Double 的格式?double = 10.66666667,如何取得小数点后2 位的数值?
- 如何把绝对路径转化为网络路径
- 在数据库中插入日期问题
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.geom.*;
import java.awt.print.*;public class SimpleBook extends JPanel implements ActionListener{ final static Color bg = Color.white;
final static Color fg = Color.black;
final static Color red = Color.red;
final static Color white = Color.white; final static BasicStroke stroke = new BasicStroke(2.0f);
final static BasicStroke wideStroke = new BasicStroke(8.0f); final static float dash1[] = {10.0f};
final static BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash1, 0.0f);
final static JButton button = new JButton("Print"); public SimpleBook() {
setBackground(bg);
button.addActionListener(this);
} public void actionPerformed(ActionEvent e) { // Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Create a landscape page format
PageFormat landscape = job.defaultPage();
landscape.setOrientation(PageFormat.LANDSCAPE);
// Set up a book
Book bk = new Book();
bk.append(new PaintCover(), job.defaultPage());
bk.append(new PaintContent(), landscape);
// Pass the book to the PrinterJob
job.setPageable(bk);
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception exc) { /* Handle Exception */ }
}
} public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawShapes(g2);
} static void drawShapes(Graphics2D g2){
int gridWidth = 600 / 6;
int gridHeight = 250 / 2;
int rowspacing = 5;
int columnspacing = 7;
int rectWidth = gridWidth - columnspacing;
int rectHeight = gridHeight - rowspacing;
Color fg3D = Color.lightGray;
g2.setPaint(fg3D);
g2.drawRect(80, 80, 605 - 1, 265);
g2.setPaint(fg);
int x = 85;
int y = 87;
// draw Line2D.Double
g2.draw(new Line2D.Double(x, y+rectHeight-1, x + rectWidth, y));
x += gridWidth;
Graphics2D temp = g2;
// draw Rectangle2D.Double
g2.setStroke(stroke);
g2.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
x += gridWidth;
// draw RoundRectangle2D.Double
g2.setStroke(dashed);
g2.draw(new RoundRectangle2D.Double(x, y, rectWidth,
rectHeight, 10, 10));
x += gridWidth;
// draw Arc2D.Double
g2.setStroke(wideStroke);
g2.draw(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
135, Arc2D.OPEN));
x += gridWidth;
// draw Ellipse2D.Double
g2.setStroke(stroke);
g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
x += gridWidth;
// draw GeneralPath (polygon)
int x1Points[] = {x, x+rectWidth, x, x+rectWidth};
int y1Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x1Points.length);
polygon.moveTo(x1Points[0], y1Points[0]);
for ( int index = 1; index < x1Points.length; index++ ) {
polygon.lineTo(x1Points[index], y1Points[index]);
};
polygon.closePath();
g2.draw(polygon); // NEW ROW
x = 85;
y += gridHeight;
// draw GeneralPath (polyline)
int x2Points[] = {x, x+rectWidth, x, x+rectWidth};
int y2Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x2Points.length);
polyline.moveTo (x2Points[0], y2Points[0]);
for ( int index = 1; index < x2Points.length; index++ ) {
polyline.lineTo(x2Points[index], y2Points[index]);
};
g2.draw(polyline);
x += gridWidth;
// fill Rectangle2D.Double (red)
g2.setPaint(red);
g2.fill(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
g2.setPaint(fg);
x += gridWidth;
// fill RoundRectangle2D.Double
GradientPaint redtowhite = new GradientPaint(x,y,red,x+rectWidth,y,white);
g2.setPaint(redtowhite);
g2.fill(new RoundRectangle2D.Double(x, y, rectWidth,
rectHeight, 10, 10));
g2.setPaint(fg);
x += gridWidth;
// fill Arc2D
g2.setPaint(red);
g2.fill(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
135, Arc2D.OPEN));
g2.setPaint(fg);
x += gridWidth;
// fill Ellipse2D.Double
redtowhite = new GradientPaint(x,y,red,x+rectWidth, y,white);
g2.setPaint(redtowhite);
g2.fill (new Ellipse2D.Double(x, y, rectWidth, rectHeight));
g2.setPaint(fg);
x += gridWidth;
// fill and stroke GeneralPath
int x3Points[] = {x, x+rectWidth, x, x+rectWidth};
int y3Points[] = {y, y+rectHeight, y+rectHeight, y};
GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
x3Points.length);
filledPolygon.moveTo(x3Points[0], y3Points[0]);
for ( int index = 1; index < x3Points.length; index++ ) {
filledPolygon.lineTo(x3Points[index], y3Points[index]);
};
filledPolygon.closePath();
g2.setPaint(red);
g2.fill(filledPolygon);
g2.setPaint(fg);
g2.draw(filledPolygon);
g2.setStroke(temp.getStroke());
} public static void main(String[] args) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
public void windowClosed(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame();
f.addWindowListener(l);
JPanel panel = new JPanel();
panel.add(button);
f.getContentPane().add(BorderLayout.SOUTH, panel);
f.getContentPane().add(BorderLayout.CENTER, new SimpleBook());
f.setSize(775, 450);
f.show();
}}class PaintCover implements Printable {
Font fnt = new Font("Helvetica-Bold", Font.PLAIN, 48); public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
g.setFont(fnt);
g.setColor(Color.black);
g.drawString("Sample Shapes", 100, 200);
return Printable.PAGE_EXISTS;
}
}class PaintContent implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
SimpleBook.drawShapes((Graphics2D) g);
return Printable.PAGE_EXISTS; }
}
C:\Documents and Settings\Administrator\桌面\SimpleBook.java:200: cannot resolve symbol
symbol : class Pageformat
location: class PaintCover
public int print(Graphics g, Pageformat pf, int pageIndex)
^
C:\Documents and Settings\Administrator\桌面\SimpleBook.java:210: cannot resolve symbol
symbol : class Pageformat
location: class PaintContent
public int print(Graphics g, Pageformat pf, int pageIndex)
^
C:\Documents and Settings\Administrator\桌面\SimpleBook.java:35: cannot resolve symbol
symbol : class Pageformat
location: class SimpleBook
Pageformat landscape = job.defaultPage();
^
C:\Documents and Settings\Administrator\桌面\SimpleBook.java:36: cannot resolve symbol
symbol : variable Pageformat
location: class SimpleBook
landscape.setOrientation(Pageformat.LANDSCAPE);
^
4 errorsProcess completed.
PageFormat
而你的出错信息里:
PageformatPageFormat与Pageformat可非等同的!这例子不是我写的,是从sun公司主站下载的:)
地址如下:
http://java.sun.com/docs/books/tutorial/2d/printing/collection.html