текст Jlabel становится обрезанным при печати

1

У меня есть 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();
    }
} 
Теги:
printing
swing

1 ответ

1

Это задача менеджера компоновки. Вы можете попробовать следующее:

label.setSize( label.getPreferredSize() );
  • 0
    попробовал это. Не сработало. В какой-то конкретный момент я должен это назвать?
  • 0
    Привет, где твои JLabels инициализированы?
Показать ещё 3 комментария

Ещё вопросы

Сообщество Overcoder
Наверх
Меню