为什么在SWT Eclipse(文本)中为我打印一行。我想在文本中打印多行。那是我的代码。从我的打印机中,我收到一条消息,而不是我的打印中的多行
Button btnPrint = new Button(shell, SWT.NONE);
btnPrint.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
PrintDialog printDialog = new PrintDialog(shell, SWT.MULTI| SWT.BORDER );
printDialog.setText("Print");
PrinterData printerData = printDialog.open();
if (!(printerData == null)) {
Printer p = new Printer(printerData);
p.startJob("PrintJob");
p.startPage();
Rectangle trim = p.computeTrim(0,0,0,0);
Point dpi = p.getDPI();
int leftMargin = dpi.x + trim.x;
int topMargin = dpi.y / 2 + trim.y;
GC gc = new GC(p);
Font font = gc.getFont();
String printText = text.getText();
Point extent = gc.stringExtent(printText);
gc.drawString(printText, leftMargin, topMargin+ font.getFontData()[0].getHeight());
p.endPage();
gc.dispose();
p.endJob();
p.dispose();
}
}
});
答案 0 :(得分:0)
gc.drawString
不查找行定界符。
使用gc.drawText
:
public void drawText(String string, int x, int y)
会查找行定界符(和制表符)。或使用
public void drawText(String string, int x, int y, int flags)
并为SWT.DRAW_DELIMITER
的值指定flags