java桌面程序怎么调用打印机打印小票啊 java桌面程序怎么调用打印机打印小票啊?像超市给顾客的那种 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class TestPrint implements Printable {/*** * @param Graphic指明打印的图形环境 * @param* PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英寸的1/72,1英寸为25.4毫米。A4纸大致为595×842点) * @param* pageIndex指明页号**/public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException { System.out.println("pageIndex=" + pageIndex); Component c = null; // print string String str = "功能测试"; // 转换成Graphics2D Graphics2D g2 = (Graphics2D) gra; // 设置打印颜色为黑色 g2.setColor(Color.red); // 打印起点坐标 double x = pf.getImageableX(); double y = pf.getImageableY(); switch (pageIndex) { case 0: // 设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称) // Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput Font font = new Font("新宋体", Font.PLAIN, 9); g2.setFont(font); // 设置字体 // BasicStroke bs_3=new BasicStroke(0.5f); float[] dash1 = { 2.0f }; // 设置打印线的属性。 // 1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量 g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f)); // g2.setStroke(bs_3);//设置线宽 float heigth = font.getSize2D(); // 字体高度 System.out.println("x=" + x); // -1- 用Graphics2D直接输出 // 首字符的基线(右下部)位于用户空间中的 (x, y) 位置处 // g2.drawLine(10,10,200,300); Image src = Toolkit .getDefaultToolkit() .getImage( "D:\\Aotori\\javaPic.jpg"); g2.drawImage(src, (int) x, (int) y, c); int img_Height = src.getHeight(c); int img_width = src.getWidth(c); //System.out.println("img_Height="+img_Height+"img_width="+img_width // ) ; g2.drawString(str, (float) x, (float) y + 1 * heigth + img_Height); //第一排 g2.drawString("oooooooooooo", (float) 50, (float) 60 + 1 * heigth + img_Height); //第二排 g2.drawString("22222222222ddddddddddddddddddddddddddd222222222222", (float) 80, (float) 90 + 1 * heigth + img_Height); //第三排 g2.drawLine((int) x, (int) (y + 1 * heigth + img_Height + 10), (int) x + 200, (int) (y + 1 * heigth + img_Height + 10)); g2.drawImage(src, (int) x, (int) (y + 1 * heigth + img_Height + 11), c); return PAGE_EXISTS; default: return NO_SUCH_PAGE; }}public static void test() { //ReadData(); // 通俗理解就是书、文档 Book book = new Book(); // 设置成竖打 PageFormat pf = new PageFormat(); pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白 // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。 Paper p = new Paper(); p.setSize(590, 840); // 纸张大小(590, 840)表示A4纸 p.setImageableArea(10, 10, 260, 343); // A4(595 X // 842)设置打印区域,其实0,0应该是72,72 // ,因为A4纸的默认X,Y边距是72 pf.setPaper(p); // 把 PageFormat 和 Printable 添加到书中,组成一个页面 book.append(new TestPrint(), pf); // 获取打印服务对象 PrinterJob job = PrinterJob.getPrinterJob(); // 设置打印类 job.setPageable(book); try { // 可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印 boolean a=job.printDialog(); if(a) { job.print(); } } catch (PrinterException e) { e.printStackTrace(); }}} 关于内存和时间消耗的问题 关于Applet类的一个问题 怎么获得从其他数据库同步过来的新的数据? 请教一个问题,为什么不显示结果呢?郁闷,谢谢! 各位大哥!!!!求救啊!~! java与sql连接 原来一直写 VC, 换公司了,要求写java, 新手上路,多多关照,散发保平安! 我想横着打,但他总是竖着打印。我已经设置了pageformat.setOrientaion(PageFormat.LANDSCAPES) ,问什么不行? 把java文件编译成可执行文件的工具是什么? 求问,这里启动线程不执行run方法了 如何对一个具体的类进行Mock 求书或者资料
public class TestPrint implements Printable {/**
* * @param Graphic指明打印的图形环境 * @param
* PageFormat指明打印页格式(页面大小以点为计量单位,1点为1英寸的1/72,1英寸为25.4毫米。A4纸大致为595×842点) * @param
* pageIndex指明页号
**/
public int print(Graphics gra, PageFormat pf, int pageIndex)
throws PrinterException {
System.out.println("pageIndex=" + pageIndex);
Component c = null;
// print string
String str = "功能测试";
// 转换成Graphics2D
Graphics2D g2 = (Graphics2D) gra;
// 设置打印颜色为黑色
g2.setColor(Color.red);
// 打印起点坐标
double x = pf.getImageableX();
double y = pf.getImageableY();
switch (pageIndex) {
case 0:
// 设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
// Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
Font font = new Font("新宋体", Font.PLAIN, 9);
g2.setFont(font); // 设置字体
// BasicStroke bs_3=new BasicStroke(0.5f);
float[] dash1 = { 2.0f };
// 设置打印线的属性。
// 1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量
g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
// g2.setStroke(bs_3);//设置线宽
float heigth = font.getSize2D(); // 字体高度
System.out.println("x=" + x);
// -1- 用Graphics2D直接输出
// 首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
// g2.drawLine(10,10,200,300);
Image src = Toolkit
.getDefaultToolkit()
.getImage(
"D:\\Aotori\\javaPic.jpg");
g2.drawImage(src, (int) x, (int) y, c);
int img_Height = src.getHeight(c);
int img_width = src.getWidth(c);
//System.out.println("img_Height="+img_Height+"img_width="+img_width
// ) ;
g2.drawString(str, (float) x, (float) y + 1 * heigth + img_Height); //第一排
g2.drawString("oooooooooooo", (float) 50, (float) 60 + 1 * heigth + img_Height); //第二排
g2.drawString("22222222222ddddddddddddddddddddddddddd222222222222", (float) 80, (float) 90 + 1 * heigth + img_Height); //第三排
g2.drawLine((int) x, (int) (y + 1 * heigth + img_Height + 10),
(int) x + 200, (int) (y + 1 * heigth + img_Height + 10));
g2.drawImage(src, (int) x,
(int) (y + 1 * heigth + img_Height + 11), c);
return PAGE_EXISTS;
default:
return NO_SUCH_PAGE;
}
}public static void test() {
//ReadData();
// 通俗理解就是书、文档
Book book = new Book();
// 设置成竖打
PageFormat pf = new PageFormat();
pf.setOrientation(PageFormat.PORTRAIT); // LANDSCAPE表示竖打;PORTRAIT表示横打;REVERSE_LANDSCAPE表示打印空白
// 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。
Paper p = new Paper();
p.setSize(590, 840); // 纸张大小(590, 840)表示A4纸
p.setImageableArea(10, 10, 260, 343); // A4(595 X
// 842)设置打印区域,其实0,0应该是72,72
// ,因为A4纸的默认X,Y边距是72
pf.setPaper(p);
// 把 PageFormat 和 Printable 添加到书中,组成一个页面
book.append(new TestPrint(), pf);
// 获取打印服务对象
PrinterJob job = PrinterJob.getPrinterJob();
// 设置打印类
job.setPageable(book);
try {
// 可以用printDialog显示打印对话框,在用户确认后打印;也可以直接打印
boolean a=job.printDialog();
if(a)
{
job.print();
}
} catch (PrinterException e) {
e.printStackTrace();
}
}
}