我有一个带有一堆jLabel和其他组件的jPanel。当我打印jPanel时,jLabel中的文本被截断。我该如何解决这个问题?
这是我的打印代码
//handle the printing
private class Prints implements Printable {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0) {
return Printable.NO_SUCH_PAGE;
}
// get the bounds of the component
Dimension dim = dashHolderPanel.getSize();
double cHeight = dim.getHeight();
double cWidth = dim.getWidth();
// get the bounds of the printable area
double pHeight = pageFormat.getImageableHeight();
double pWidth = pageFormat.getImageableWidth();
double pXStart = pageFormat.getImageableX();
double pYStart = pageFormat.getImageableY();
double xRatio = pWidth / cWidth;
double yRatio = pHeight / cHeight;
Graphics2D g2 = (Graphics2D) graphics;
g2.translate(pXStart, pYStart);
g2.scale(xRatio, yRatio);
// print it
dashHolderPanel.print(g2);
return Printable.PAGE_EXISTS;
}
}
以下是打印按钮的代码
private void printDashButtonMouseClicked(java.awt.event.MouseEvent evt) {
try {
//print the dash
if (evt.getClickCount() == 1) {
PrinterJob pjob = PrinterJob.getPrinterJob();
pjob.setJobName("Print ROP Dash");
//set the attributes of the page formatter
PrintRequestAttributeSet attr_set = new HashPrintRequestAttributeSet();
attr_set.add(Fidelity.FIDELITY_TRUE);
attr_set.add(PrintQuality.HIGH);
attr_set.add(PrintQuality.HIGH);
attr_set.add(OrientationRequested.LANDSCAPE);
attr_set.add(MediaSizeName.NA_LETTER);
float width = (float) MediaSize.NA.LETTER.getX(MediaSize.INCH);
float height = (float) MediaSize.NA.LETTER.getY(MediaSize.INCH);
attr_set.add(new MediaPrintableArea(0.25f, 0.25f, width - 0.5f, height - 0.5f, MediaPrintableArea.INCH));
//get the formatter
PageFormat pf2 = pjob.pageDialog(attr_set);
//If user does not hit cancel then print.
if (pf2 != null && pjob.printDialog() == true) {
//Set print component
pjob.setPrintable(new Prints(), pf2);
//print the dash
pjob.print();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
这是布局管理器的工作。你可以试试这个:
label.setSize( label.getPreferredSize() );