Mr bangq請教你一個java列印的問題!
下面這段程式碼執行除錯都沒問題,問題出在,列印的時候測試文字都
打不出來,印表機直接就跳掉了,我用的印表機是Epson LQ-300k+ ESC/p 2,大家幫幫忙啊,謝謝!
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.swing.*;
public class PrintTest
{
public static void main(String[] args)
{
JFrame frame = new PrintTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class PrintTestFrame extends JFrame
{
public PrintTestFrame()
{
setTitle("PrintTest");
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
canvas = new PrintPanel();
contentPane.add(canvas, BorderLayout.CENTER);
attributes = new HashPrintRequestAttributeSet();
JPanel buttonPanel = new JPanel();
JButton printButton = new JButton("Print");
buttonPanel.add(printButton);
printButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat = job.defaultPage();//得到預設頁格式
Paper paper = pageFormat.getPaper();//得到頁面格式的紙張
paper.setSize(paper.getWidth(),paper.getHeight());//紙張大小
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
pageFormat.setPaper(paper);//將該紙張作為格式
job.setPrintable(canvas,pageFormat);//不止提供列印內容,還有格式
if (job.printDialog())
{
job.print();
}
}
catch (PrinterException exception)
{
JOptionPane.showMessageDialog(
PrintTestFrame.this, exception);
}
}
});
JButton pageSetupButton = new JButton("Page setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob job = PrinterJob.getPrinterJob();
job.pageDialog(attributes);
}
});
JButton printPreviewButton = new JButton("Print preview");
buttonPanel.add(printPreviewButton);
printPreviewButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat = job.defaultPage();//得到預設頁格式
Paper paper = pageFormat.getPaper();//得到頁面格式的紙張
paper.setSize(paper.getWidth(),paper.getHeight());
System.out.println(paper.getWidth());
System.out.println(paper.getHeight());
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
pageFormat.setPaper(paper);//將該紙張作為格式
job.setPrintable(canvas,pageFormat);
PrintPreviewDialog dialog = new PrintPreviewDialog(canvas,pageFormat,1);
dialog.show();
}
});
contentPane.add(buttonPanel, BorderLayout.NORTH);
}
private PrintPanel canvas;
private PrintRequestAttributeSet attributes;
private static final int WIDTH = 500;
private static final int HEIGHT=400;
}
class PrintPanel extends JPanel implements Printable
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawPage(g);
}
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException
{
if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics g2 = g;
g2.translate(0,0);
drawPage(g);
return Printable.PAGE_EXISTS;
}
public void drawPage(Graphics g2)
{
Font f = new Font("楷體_GB2312",Font.PLAIN, 12);
g2.setFont(f);
g2.drawString("測試一下",290,110);
g2.drawString("測試一下下",300,150);
}
}
class PrintPreviewDialog extends JDialog
{
public PrintPreviewDialog(Printable p, PageFormat pf,
int pages)
{
Book book = new Book();
book.append(p, pf, pages);
layoutUI(book);
}
public PrintPreviewDialog(Book b)
{
layoutUI(b);
}
public void layoutUI(Book book)
{
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
canvas = new PrintPreviewCanvas(book);
contentPane.add(canvas, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton nextButton = new JButton("Next");
buttonPanel.add(nextButton);
nextButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
canvas.flipPage(1);
}
});
JButton previousButton = new JButton("Previous");
buttonPanel.add(previousButton);
previousButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
canvas.flipPage(-1);
}
});
JButton closeButton = new JButton("Close");
buttonPanel.add(closeButton);
closeButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
contentPane.add(buttonPanel, BorderLayout.SOUTH);
}
private PrintPreviewCanvas canvas;
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
}
class PrintPreviewCanvas extends JPanel
{
public PrintPreviewCanvas(Book b)
{
book = b;
currentPage = 0;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
PageFormat pageFormat = book.getPageFormat(currentPage);
double xoff; // x offset of page start in window
double yoff; // y offset of page start in window
double scale; // scale factor to fit page in window
double px = pageFormat.getWidth();
double py = pageFormat.getHeight();
double sx = getWidth() - 1;
double sy = getHeight() - 1;
if (px / py < sx / sy) // center horizontally
{
scale = sy / py;
xoff = 0.5 * (sx - scale * px);
yoff = 0;
}
else // center vertically
{
scale = sx / px;
xoff = 0;
yoff = 0.5 * (sy - scale * py);
}
g2.translate((float)xoff, (float)yoff);
g2.scale((float)scale, (float)scale);
// draw page outline (ignoring margins)
Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
g2.setPaint(Color.white);
g2.fill(page);
g2.setPaint(Color.black);
g2.draw(page);
Printable printable = book.getPrintable(currentPage);
try
{
printable.print(g2, pageFormat, currentPage);
}
catch (PrinterException exception)
{
g2.draw(new Line2D.Double(0, 0, px, py));
g2.draw(new Line2D.Double(0, px, 0, py));
}
}
public void flipPage(int by)
{
int newPage = currentPage + by;
if (0 <= newPage && newPage < book.getNumberOfPages())
{
currentPage = newPage;
repaint();
}
}
private Book book;
private int currentPage;
}
打不出來,印表機直接就跳掉了,我用的印表機是Epson LQ-300k+ ESC/p 2,大家幫幫忙啊,謝謝!
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.util.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.swing.*;
public class PrintTest
{
public static void main(String[] args)
{
JFrame frame = new PrintTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class PrintTestFrame extends JFrame
{
public PrintTestFrame()
{
setTitle("PrintTest");
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
canvas = new PrintPanel();
contentPane.add(canvas, BorderLayout.CENTER);
attributes = new HashPrintRequestAttributeSet();
JPanel buttonPanel = new JPanel();
JButton printButton = new JButton("Print");
buttonPanel.add(printButton);
printButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat = job.defaultPage();//得到預設頁格式
Paper paper = pageFormat.getPaper();//得到頁面格式的紙張
paper.setSize(paper.getWidth(),paper.getHeight());//紙張大小
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
pageFormat.setPaper(paper);//將該紙張作為格式
job.setPrintable(canvas,pageFormat);//不止提供列印內容,還有格式
if (job.printDialog())
{
job.print();
}
}
catch (PrinterException exception)
{
JOptionPane.showMessageDialog(
PrintTestFrame.this, exception);
}
}
});
JButton pageSetupButton = new JButton("Page setup");
buttonPanel.add(pageSetupButton);
pageSetupButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob job = PrinterJob.getPrinterJob();
job.pageDialog(attributes);
}
});
JButton printPreviewButton = new JButton("Print preview");
buttonPanel.add(printPreviewButton);
printPreviewButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat pageFormat = job.defaultPage();//得到預設頁格式
Paper paper = pageFormat.getPaper();//得到頁面格式的紙張
paper.setSize(paper.getWidth(),paper.getHeight());
System.out.println(paper.getWidth());
System.out.println(paper.getHeight());
paper.setImageableArea(0,0,paper.getWidth(),paper.getHeight());
pageFormat.setPaper(paper);//將該紙張作為格式
job.setPrintable(canvas,pageFormat);
PrintPreviewDialog dialog = new PrintPreviewDialog(canvas,pageFormat,1);
dialog.show();
}
});
contentPane.add(buttonPanel, BorderLayout.NORTH);
}
private PrintPanel canvas;
private PrintRequestAttributeSet attributes;
private static final int WIDTH = 500;
private static final int HEIGHT=400;
}
class PrintPanel extends JPanel implements Printable
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawPage(g);
}
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException
{
if (page >= 1) return Printable.NO_SUCH_PAGE;
Graphics g2 = g;
g2.translate(0,0);
drawPage(g);
return Printable.PAGE_EXISTS;
}
public void drawPage(Graphics g2)
{
Font f = new Font("楷體_GB2312",Font.PLAIN, 12);
g2.setFont(f);
g2.drawString("測試一下",290,110);
g2.drawString("測試一下下",300,150);
}
}
class PrintPreviewDialog extends JDialog
{
public PrintPreviewDialog(Printable p, PageFormat pf,
int pages)
{
Book book = new Book();
book.append(p, pf, pages);
layoutUI(book);
}
public PrintPreviewDialog(Book b)
{
layoutUI(b);
}
public void layoutUI(Book book)
{
setSize(WIDTH, HEIGHT);
Container contentPane = getContentPane();
canvas = new PrintPreviewCanvas(book);
contentPane.add(canvas, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton nextButton = new JButton("Next");
buttonPanel.add(nextButton);
nextButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
canvas.flipPage(1);
}
});
JButton previousButton = new JButton("Previous");
buttonPanel.add(previousButton);
previousButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
canvas.flipPage(-1);
}
});
JButton closeButton = new JButton("Close");
buttonPanel.add(closeButton);
closeButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setVisible(false);
}
});
contentPane.add(buttonPanel, BorderLayout.SOUTH);
}
private PrintPreviewCanvas canvas;
private static final int WIDTH = 300;
private static final int HEIGHT = 300;
}
class PrintPreviewCanvas extends JPanel
{
public PrintPreviewCanvas(Book b)
{
book = b;
currentPage = 0;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
PageFormat pageFormat = book.getPageFormat(currentPage);
double xoff; // x offset of page start in window
double yoff; // y offset of page start in window
double scale; // scale factor to fit page in window
double px = pageFormat.getWidth();
double py = pageFormat.getHeight();
double sx = getWidth() - 1;
double sy = getHeight() - 1;
if (px / py < sx / sy) // center horizontally
{
scale = sy / py;
xoff = 0.5 * (sx - scale * px);
yoff = 0;
}
else // center vertically
{
scale = sx / px;
xoff = 0;
yoff = 0.5 * (sy - scale * py);
}
g2.translate((float)xoff, (float)yoff);
g2.scale((float)scale, (float)scale);
// draw page outline (ignoring margins)
Rectangle2D page = new Rectangle2D.Double(0, 0, px, py);
g2.setPaint(Color.white);
g2.fill(page);
g2.setPaint(Color.black);
g2.draw(page);
Printable printable = book.getPrintable(currentPage);
try
{
printable.print(g2, pageFormat, currentPage);
}
catch (PrinterException exception)
{
g2.draw(new Line2D.Double(0, 0, px, py));
g2.draw(new Line2D.Double(0, px, 0, py));
}
}
public void flipPage(int by)
{
int newPage = currentPage + by;
if (0 <= newPage && newPage < book.getNumberOfPages())
{
currentPage = newPage;
repaint();
}
}
private Book book;
private int currentPage;
}
相關文章
- 請問一個java nio問題Java
- 請教大家一個JAVA的問題Java
- 【求助】請教一個初學java的小問題Java
- 請教一個cookies的問題Cookie
- 請教一個executeBatch()的問題BAT
- 請教下bangq老師,兩個概念:抽象類和介面抽象
- 請問在JAVA WEB專案中一個比較棘手的問題JavaWeb
- 請教一個java程式記憶體釋放的問題Java記憶體
- 請教一個JVM問題JVM
- 請教高手一個問題!
- 請教一個struct tag的問題Struct
- 請教一個ADAPTER的問題APT
- 請教一個WEB START的問題!Web
- 請問一個有關jdbc效能的問題JDBC
- 請問一個jndi連線的小問題
- 我是剛開學JAVA,想請教一個問題。Java
- 求教 一個java取php session的問題 急 請板橋里人指教~~~JavaPHPSession
- 請教一個關於JAVA SOCKET程式設計中的問題Java程式設計
- 請教各位高手一個問題
- 請教一個go切片引用的問題Go
- 請教一個物件設計的問題物件
- 求問一個 swipe 的問題,請大神們指教
- 請問一個mysql使用者管理的 問題。MySql
- 一個java加密引起的問題Java加密
- 一個奇怪的Java集合問題Java
- 問一個小的運算問題,請高手指教!
- 請問老師一個關於命令模式的問題模式
- 請教一個多執行緒的問題執行緒
- 請教Banq一個Ruby架構的問題架構
- 請教各位大俠一個JavaBean的問題JavaBean
- 請教一個DAO設計模式的問題設計模式
- 請教工廠方法的一個應用問題
- 請教一個檔案上傳的問題
- 請教一個timeout的實現問題
- 請教一個演算法問題演算法
- 請教一個observer設計問題。Server
- 請問一個關於web的管理系統的問題Web
- 回覆java_gh的一個問題Java