簡體   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