您的位置:

java代码实现打印,JAVA代码实现打印九九乘法表

本文目录一览:

Java打印功能怎么实现

import java.awt.*;import java.awt.event.*;public class Example7_37{ public static void main(String args[]) { MyFrame f=new MyFrame(); f.setBounds(70,70,70,89);f.setVisible(true);f.validate(); }}class MyFrame extends Frame implements ActionListener{ PrintJob p=null; //声明一个PrintJob对象。 Graphics g=null; TextArea text=new TextArea(10,10); Button 打印文本框=new Button("打印文本框"), 打印窗口=new Button("打印窗口"), 打印按扭=new Button("打印按扭"); MyFrame() { super("在应用程序中打印"); 打印文本框.addActionListener(this); 打印窗口.addActionListener(this); 打印按扭.addActionListener(this); add(text,"Center"); Panel panel=new Panel(); panel.add(打印文本框); panel.add(打印窗口); panel.add(打印按扭); add(panel,"South"); addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0); } }); }public void actionPerformed(ActionEvent e) { if(e.getSource()==打印文本框) { p=getToolkit().getPrintJob(this,"ok",null); g=p.getGraphics(); g.translate(120,200); text.printAll(g); g.dispose(); p.end(); } else if(e.getSource()==打印窗口) { p=getToolkit().getPrintJob(this,"ok",null); g=p.getGraphics(); g.translate(120,200); this.printAll(g); g.dispose(); p.end(); } else if(e.getSource()==打印按扭) { p=getToolkit().getPrintJob(this,"ok",null); g=p.getGraphics(); g.translate(120,200); 打印文本框.printAll(g); g.translate(78,0); 打印窗口.printAll(g); g.translate(66,0); 打印按扭.printAll(g); g.dispose(); p.end(); } }}

如何通过JAVA代码调用网络打印机使其打印接收到的文件

用java实现本地打印:java.awt中提供了一些打印的API,要实现打印,首先要获得打印对象,然后继承Printable实现接口方法print,以便打印机进行打印,最后用用Graphics2D直接输出直接输出。下面代码实现了简单的打印功能:

java 中如何实现打印

js:object id="WebBrowser" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height="0" width="0" /object

script type="text/javascript"

function userPrintExec()

{

document.WebBrowser.Execwb(7,1);//打印预览

return false;

}

function userPrint(){

// var name=document.getElementById("name").value;

newwin=window.open("aa.jsp?biaoshi=11111name=aa"); 这里改成你的name就行了

if(document.all){

newwin.moveTo(0,0)//新窗口的坐标

newwin.resizeTo(screen.width,screen.height)

//此处可设置窗口大小,如(1024, 768),不设置的话为全屏

}

return false;

}

/script body里加: body %if(request.getParameter("biaoshi")!=null) {request.removeAttribute("biaoshi"); % onload="return userPrintExec();"%}% 打印按钮: input type="button" name="Sub" value="打印" OnClick="return userPrint()" style="float:right;"/ 可以的,接分!

JAVA中怎么编写打印功能代码

Print.java--打印内容定义 [code] import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.event.*; import java.util.*; import java.io.*; public class Print implements Printable{ int m_wPage; int m_hPage; int m_orientation; Printable m_target; int maxNumPage=1; String title="数据表格打印"; Font titleFont=new Font("黑体",Font.BOLD,14); boolean hasTail=true; int tailAlign=0; int headAlign=0; int topSpace=0; int leftSpace=0; int yStart=0; int yEnd=0; int xStart=topSpace; int xEnd=0; int x=0,y=0; String strTemp="打印内容"; public void doPrint(){ try{ m_orientation=PageFormat.PORTRAIT; //设置打印对象,默认纸张 PrinterJob prnJob=PrinterJob.getPrinterJob(); PageFormat pageFormat=prnJob.defaultPage(); pageFormat.setOrientation(m_orientation); m_wPage=(int)(pageFormat.getWidth()); m_hPage=(int)(pageFormat.getHeight()); //将待打印的窗体根据默认纸张设置传入打印对象 prnJob.setPrintable(this,pageFormat); if(!prnJob.printDialog()) return; prnJob.print(); }catch(PrinterException ex){ ex.printStackTrace(); System.err.println("打印错误:"+ex.toString()); } } /** * 初始化打印参数 */ public void initPrintParameter() { } /** *构造打印内容,以送打印机打印 */ public int print(Graphics pg,PageFormat pageFormat, int pageIndex) throws PrinterException{ //初始化打印参数 initPrintParameter(); //将画布设置为页面大小 pg.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY()); int wPage=0; int hPage=0; //根据打印机页面设置调整画布大小 if(pageFormat.getOrientation()==pageFormat.PORTRAIT){ wPage=(int)pageFormat.getImageableWidth(); hPage=(int)pageFormat.getImageableHeight(); } else{ wPage=(int)pageFormat.getImageableWidth(); wPage+=wPage/2; hPage=(int)pageFormat.getImageableHeight(); pg.setClip(0,0,wPage,hPage); } wPage=wPage-2*leftSpace; hPage=hPage-2*topSpace; xStart=leftSpace; xEnd=wPage-2; //为画布设置颜色和字体 int y=topSpace; pg.setFont(titleFont); pg.setColor(Color.black); //画标题,并使其居中 Font fn=pg.getFont(); FontMetrics fm=pg.getFontMetrics(); y+=fm.getAscent(); alignText(title,pg,y,xStart,xEnd,headAlign); y+=30; x=leftSpace+2; Font headerFont=new Font("宋体",Font.BOLD,14); pg.setFont(headerFont); fm=pg.getFontMetrics(); int h=fm.getAscent(); yStart=y-1; y+=h; pg.setFont(headerFont); fm=pg.getFontMetrics(); int header=y; h=fm.getHeight(); //计算行高,每页行数,总行数和指定页码的起始行、结束行 int rowH=Math.max(h,10); int tailH=rowH+30; int rowPerPage=0; int leftPix=0; if(hasTail){ rowPerPage=(hPage-header-tailH)/rowH; leftPix=(hPage-header-tailH)%rowH; yEnd=hPage-leftPix-tailH+2; } else{ rowPerPage=(hPage-header)/rowH; leftPix=(hPage-header)%rowH; yEnd=hPage-leftPix+2; } pg.drawString(strTemp,x,y); //画表格边框 pg.drawLine(xStart,yStart,xStart,yEnd); pg.drawLine(xStart,yStart,xEnd,yStart); pg.drawLine(xEnd,yStart,xEnd,yEnd); pg.drawLine(xStart,yEnd,xEnd,yEnd); //打印页码 if(hasTail){ int pageNumber=pageIndex+1; String s="第"+pageNumber+"页"; alignText(s,pg,yEnd+30,xStart,xEnd,tailAlign); } System.gc(); return PAGE_EXISTS; } /** * 文字排列,坐标在y处,显示范围(start-end) * 0表示居中显示,1表示左对齐,2表示右对齐 */ private void alignText(String s,Graphics pg,int y,int start, int end,int mode){ Font fn=pg.getFont(); FontMetrics fm=pg.getFontMetrics(); int wString=fm.stringWidth(s); int x=start; switch(mode) { case 0: if((end-start-wString)0) x=start+(end-start-wString)/2; break; case 1: break; case 2: if((end-start-wString)0) x=start+(end-start-wString); break; } pg.drawString(s,x,y); } public static void main(String[] args){ Print p=new Print(); p.doPrint(); } } [code] 运行方法: javac -d . Print.java java Print 自己运行一下 from:

采纳哦