有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java向打印机发送文本框控件?

我似乎不知道如何用Java发送文本框控件进行打印

该程序创建单词搜索谜题,现在我想打印它们

我只是想按原样打印文本框控件。。。但我不得不尝试以下其他方法

我的第一个问题是,当我试图发送它txtEasy。getText()它试图打印 一根很长的绳子。这不是我想要的。所以我不得不用一根很长的绳子 然后像这样解析出来。15字乘20字的行。不幸的是,你在这里看不到。但它是一个15乘20的盒子

  M  S  N  O  I  T  I  S  O  P  M  O  C  L  G  
  D  R  B  P  U  D  C  K  S  Q  G  B  E  I  Q
  C  D  I  N  M  B  P  I  U  N  P  M  J  J  U
  T  N  L  I  N  C  V  D  Q  B  W  I  U  A  U
  T  O  R  N  T  F  J  O  D  N  G  T  E  L  G
  B  R  K  C  O  M  M  S  W  G  H  T  S  A  N
  S  I  G  U  K  X  U  X  S  E  P  I  E  Q  C
  T  G  C  B  O  X  H  J  N  B  D  T  T  J  P
  I  I  A  A  R  Q  Y  C  S  W  I  F  O  L  M
  M  N  R  T  I  A  E  L  P  S  P  D  Q  O  U
  U  A  P  E  H  F  I  T  S  O  F  Q  N  L  P
  L  L  H  D  O  Q  F  A  O  A  G  T  Q  A  B
  A  S  S  R  W  T  C  L  T  N  P  G  E  N  S
  T  X  T  Y  P  O  V  T  I  E  W  L  Q  C  F
  I  H  S  E  J  F  E  M  L  A  D  N  D  A  E
  N  W  K  T  Q  N  E  I  I  K  I  J  V  S  I
  G  F  Y  H  S  E  E  P  I  Y  J  U  S  H  R
  D  F  V  N  D  R  D  I  B  E  F  S  R  I  U
  H  U  Y  E  V  S  F  O  E  Q  J  X  V  R  N
  V  V  R  G  R  L  G  Q  S  B  M  F  G  E  J

我的代码如下

由于目前正在设置,我将文本作为txtEasy发送到printer类。getText()

我现在用这种方法遇到的问题是字体没有正确对齐

所以我的两个问题

1)是否有办法将文本框控件发送到打印机

2)有没有办法格式化字符串字体?字体大小导致我的打印不均匀

谢谢你的帮助

private void btnEasyPrintActionPerformed(java.awt.event.ActionEvent evt) {                                             

        //System.out.println(txtEasy.getText());
        PrinterJob job = PrinterJob.getPrinterJob();
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        PageFormat pf = job.pageDialog(aset);
        job.setPrintable(new PrintPuzzle(txtEasy.getText()), pf);
        boolean ok = job.printDialog(aset);
        if (ok) {
            try {
                 job.print(aset);
            } catch (PrinterException ex) {
             /* The job did not successfully complete */
            }
        }



//******** THE PRINT CLASS ********

import java.awt.*;
import java.awt.print.*;

public class PrintPuzzle implements Printable
{
    String toPrint;
    String formatedToPrint;

    public PrintPuzzle(String item)
    {
        toPrint = item;
        //toPrint = item;
        //formatedToPrint = formatBoardToPrint(toPrint);

    }
    public int print(Graphics g, PageFormat pf, int page) throws
                                                     PrinterException
    {

        if (page > 0)
        {
            return NO_SUCH_PAGE;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());

        /* Now we perform our rendering */

        String paintMe = "";
        int YPosition = 20;
        int first = 2;
        int last = 46;

        System.out.println(toPrint);
        System.out.println("String Length is :" + toPrint.length());
        for(int i = 0;i < 20;i++)
        {
            paintMe = toPrint.substring(first, last);
            paintMe.
            System.out.print(paintMe + "First Position : " + first + " Last Position : " + last + " YPosition : " + YPosition);

            g.drawString(paintMe, 20, YPosition);

            System.out.println();
            first += 46;
            last += 46;
            YPosition += 10;

        }//end for

        //g.drawString(toPrint, 20, 20);
        //g.drawString(toPrint, 20, 30);


        /* tell the caller that this page is part of the printed document */
        return PAGE_EXISTS;
    }

}

共 (0) 个答案