繁体   English   中英

jlabel文本在打印时被截断

[英]jlabel text getting cutoff when printing

我有一个jPanel,其中包含一堆jLabel和其他组件。 当我打印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();
    }
} 

那是布局经理的工作。 您可以尝试以下方法:

label.setSize( label.getPreferredSize() );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM