【文艺复兴之swing】java代码控制打印机打印 不会弹出打印机选择框(swing)

 public static void main(String[] args) {

        Printable printable = new Printable() {
            public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
            // ""里面输入要打印的字  打印图片的话用drawImage 
                graphics.drawString(".", 1, 1);
                return 0;
            }
        };
        Book book = new Book();
        PageFormat pf = new PageFormat();
        pf.setOrientation(1);
        Paper p = new Paper();
        p.setSize(250.0D, 570.0D);
        p.setImageableArea(0.0D, 0.0D, 250.0D, 570.0D);
        pf.setPaper(p);
        book.append(printable, pf);
        PrinterJob job = PrinterJob.getPrinterJob();
        job.setPageable(book);
        HashAttributeSet hs = new HashAttributeSet();
        // 最好将打印机名称写成配置文件
         //   printerName = String.valueOf(new xxx().getPrinterName().get("printerName1"));
       String printerName = "打印机的名称";
        PrintService[] pss = PrintServiceLookup.lookupPrintServices((DocFlavor)null, hs);
        
			
            hs.add(new PrinterName(printerName, (Locale) null));
            pss = PrintServiceLookup.lookupPrintServices((DocFlavor) null, hs);

            job.setPageable(book);
			job.setPrintService(pss[0]);
            job.print();
            // 需要try catch
}	


// 另外还有指令打印的方式(不需要驱动): 指令打印一般需要区分打印机连接方式:例如  网口、串口  ;
// 而上述方法需要区分打印机名称	 
/* 网口:打印机插一根类似网线的 连接电脑 :核心思路 socket 连接打印机的ip(在设备 打印机属性 端口 TCP/IP设置中可以设置打印机ip 端口)

串口(包括USB转串): 核心思路: 需要准备的资源:RXTXcomm.jar包  rxtxParallel.dll rxtxSerial.dll
*/

```java
//串口举例
  public static void main(String[] args) {
        Enumeration tempPortList;  //枚举类
        CommPortIdentifier portIp;
        tempPortList=CommPortIdentifier.getPortIdentifiers();
        while(tempPortList.hasMoreElements()){
           
            portIp=(CommPortIdentifier) tempPortList.nextElement();
            portList.add(portIp);
            int portType = portIp.getPortType();
            // 找到串口 并且名称相符的
            if (portType == (CommPortIdentifier.PORT_SERIAL) && portIp.getName().equals("COM6")){
                try {
                // 同样的代码 这里有时会报错:未知程序 原因至今未找到 
                    serialPort = (SerialPort) portIp.open("COM6",2000);
                    serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                    outputStream = serialPort.getOutputStream();
                    OutputStreamWriter writer = new OutputStreamWriter(outputStream,"GBK");
                    // 指令操作核心(网口同理): outputStream.write(打印机指令)
                    byte []cmd = new byte[3];
                    cmd[0] = 0x1B;
                    cmd[1] = 'J';
                    cmd[2] = 0x0D;
                    outputStream.write(cmd);
                    outputStream.write(1000);
                    writer.write("nihao");
                    writer.close();
                    outputStream.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
      
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟秋与你

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值