gpt4 book ai didi

java - 将文本框控件发送到打印机?

转载 作者:行者123 更新时间:2023-12-01 16:09:10 25 4
gpt4 key购买 nike

我似乎无法弄清楚如何发送文本框控件以在 Java 中进行打印。

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

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

我的第一个问题是当我尝试发送 txtEasy.getText() 时它尝试打印一根很长的绳子。这不是我想要的。所以我不得不拿一根很长的绳子并像这样解析它。 15 个单词到 20 个单词的行。不幸的是,您在这里看不到这一点。但它是一个 15 x 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

My code is as follows.

As it is currently setup, I sent the text to the printer class as txtEasy.getText()

The Problem I am now having with this method is the font is not aligned the puzzle correctly.

So my two questions...

1) is there a way to sent the Text Box Control to the printer?

2) is there a way to format a Strings font? The font size is causing my uneven prints.

Thank you for any help.

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;
}

}

最佳答案

你的 print() 函数实际上打印了一个矩阵,只是没有正确对齐吗?

调用 g2d.setFont() 并设置像 Courier 这样的固定宽度字体,这样就可以了。

您还可以调用 JTextBox.print(),它将显示一个对话框并打印内容。 More info here.

关于java - 将文本框控件发送到打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1849555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com