使用setVisible打印JFrame(false)

时间:2017-10-27 06:59:39

标签: java swing printing jframe

我创建了一个带有2个JFrame窗口的Swing应用程序,我想将第1帧作为主页面。我在第1帧设置打印按钮以打印第2帧。

如何使用frame.setVisible(false);打印第二帧?我该如何解决?

我把我的代码放在下面:

package printuiwindow;

    /**
     *
     * @author Saravanan Ponnusamy
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.print.*;

     class PrintUIWindow implements Printable, ActionListener {


    JFrame frameToPrint;

    public int print(Graphics g, PageFormat pf, int page) throws
                                                        PrinterException {

        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY()-55);

        frameToPrint.print(g);

        return PAGE_EXISTS;
    }

    public void actionPerformed(ActionEvent e) {
         PrinterJob job = PrinterJob.getPrinterJob();
         job.setPrintable(this);
         boolean ok = job.printDialog();
         if (ok) {
             try {
                  job.print();
             } catch (PrinterException ex) {
 System.out.println(ex);
             }
         }
    }

    public PrintUIWindow(JFrame f) {
        frameToPrint = f;
    }

    public static void main(String args[]) {
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        JFrame f = new JFrame("Print UI Example");
        f.addWindowListener(new WindowAdapter() {
           public void windowClosing(WindowEvent e) {System.exit(0);}
        });
 //Printing frame design start


        JFrame frame = new JFrame("Print UI Example");
         JLabel label11=new JLabel("Selling Bill",JLabel.CENTER);
        JLabel label21=new JLabel("Customer Name :",JLabel.LEFT);
        JLabel label31=new JLabel("Buying Date :",JLabel.LEFT);
        JLabel label41=new JLabel("Book Buyed :",JLabel.LEFT);
        JLabel label51=new JLabel("Number :",JLabel.LEFT);
        JLabel label61=new JLabel("Total Price :",JLabel.LEFT);
         label11.setFont(new Font("Courier New", Font.BOLD, 13));
        label21.setFont(new Font("Courier New", Font.BOLD, 13));
        label31.setFont(new Font("Courier New", Font.BOLD, 13));
        label41.setFont(new Font("Courier New", Font.BOLD, 13));
        label51.setFont(new Font("Courier New", Font.BOLD, 13));
        label61.setFont(new Font("Courier New", Font.BOLD, 13));
 JPanel panel1=new JPanel();
 panel1.setLayout(new GridLayout(6,1));
        panel1.add(label11);
        panel1.add(label21);
        panel1.add(label31);
        panel1.add(label41);
        panel1.add(label51);
        panel1.add(label61);
        frame.setSize(300,300);
        frame.setLocationRelativeTo(null);
        frame.add(panel1,BorderLayout.CENTER);  
        panel1.setBackground(Color.WHITE);
        frame.setResizable(false);
        frame.setVisible(true);
    //printing frame design end    

//first frame design start
        JLabel label1=new JLabel("Selling Bill",JLabel.CENTER);
        JLabel label2=new JLabel("Customer Name :",JLabel.LEFT);
        JLabel label3=new JLabel("Buying Date :",JLabel.LEFT);
        JLabel label4=new JLabel("Book Buyed :",JLabel.LEFT);
        JLabel label5=new JLabel("Number :",JLabel.LEFT);
        JLabel label6=new JLabel("Total Price :",JLabel.LEFT);

        label1.setFont(new Font("Courier New", Font.BOLD, 13));
        label2.setFont(new Font("Courier New", Font.BOLD, 13));
        label3.setFont(new Font("Courier New", Font.BOLD, 13));
        label4.setFont(new Font("Courier New", Font.BOLD, 13));
        label5.setFont(new Font("Courier New", Font.BOLD, 13));
        label6.setFont(new Font("Courier New", Font.BOLD, 13));

        JButton printButton = new JButton("Print This Window");

        //print button code
        printButton.addActionListener(new PrintUIWindow(frame));
        JPanel panel=new JPanel();

        panel.setLayout(new GridLayout(6,1));
        panel.add(label1);
        panel.add(label2);
        panel.add(label3);
        panel.add(label4);
        panel.add(label5);
        panel.add(label6);
        f.setSize(300,300);
        f.setLocationRelativeTo(null);
        f.add(panel,BorderLayout.CENTER);
        f.add(printButton,BorderLayout.SOUTH);
        panel.setBackground(Color.WHITE);
        f.setResizable(false);
        f.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:3)

我非常讨厌打印组件,要么学会用手工制作,要么使用像Jasper Reports这样的东西。

你有一系列问题......

  1. 组件只能有一个父组件,因此当您创建第二个窗口并向其添加组件时,您将从第一个窗口中删除它们;
  2. 你不能画一个看不见的组件;
  3. 你真的不应该打印框架,而是更好地打印面板;
  4. 除非在屏幕上实现,否则您将负责确保组件在打印前正确布局
  5. 你真的不想打印框架,你实际上想要打印面板,它只是更简单,你没有得到框架。如果您还想打印框架,则需要使框架可见。

    所以,基于this previous answer

    The window The form

    因此,基本上,这会添加一个简单的工厂方法来创建基本面板。这将生成此面板的两个实例,一个用于打印,一个用于显示(从技术上讲,您可以使用一个,但是您可以理解)。

    打印过程将在打印时更新面板的布局,以确保它的内容正确放置,以便实际渲染它们。

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.print.PageFormat;
    import java.awt.print.Printable;
    import static java.awt.print.Printable.NO_SUCH_PAGE;
    import static java.awt.print.Printable.PAGE_EXISTS;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    
    class PrintUIWindow implements Printable, ActionListener {
    
        JPanel frameToPrint;
        boolean fill = false;
    
        public int print(Graphics g, PageFormat pf, int page) throws
                        PrinterException {
    
            if (page > 0) {
                return NO_SUCH_PAGE;
            }
            double width = (int) Math.floor(pf.getImageableWidth());
            double height = (int) Math.floor(pf.getImageableHeight());
    
            if (!fill) {
    
                width = Math.min(width, frameToPrint.getPreferredSize().width);
                height = Math.min(height, frameToPrint.getPreferredSize().height);
    
            }
    
            System.out.println(width + "x" + height);
            Graphics2D g2d = (Graphics2D) g;
            g2d.translate(pf.getImageableX(), pf.getImageableY());
            System.out.println(width + "x" + height);
            frameToPrint.setBounds(0, 0, (int) Math.floor(width), (int) Math.floor(height));
            if (frameToPrint.getParent() == null) {
                frameToPrint.addNotify();
            }
            frameToPrint.validate();
            frameToPrint.doLayout();
            frameToPrint.printAll(g2d);
            if (frameToPrint.getParent() != null) {
                frameToPrint.removeNotify();
            }
    
            return PAGE_EXISTS;
        }
    
        public void actionPerformed(ActionEvent e) {
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintable(this);
            boolean ok = job.printDialog();
            if (ok) {
                try {
                    job.print();
                } catch (PrinterException ex) {
                    System.out.println(ex);
                }
            }
        }
    
        public PrintUIWindow(JPanel f) {
            frameToPrint = f;
        }
    
        public static void forceLayout(JPanel panel) {
            if (panel.getParent() == null) {
                panel.addNotify();
            }
            panel.validate();
            panel.doLayout();
            if (panel.getParent() != null) {
                panel.removeNotify();
            }
        }
    
        public static JPanel makePanel() {
            JLabel label11 = new JLabel("Selling Bill", JLabel.LEFT);
            JLabel label21 = new JLabel("Customer Name :", JLabel.LEFT);
            JLabel label31 = new JLabel("Buying Date :", JLabel.LEFT);
            JLabel label41 = new JLabel("Book Buyed :", JLabel.LEFT);
            JLabel label51 = new JLabel("Number :", JLabel.LEFT);
            JLabel label61 = new JLabel("Total Price :", JLabel.LEFT);
            label11.setFont(new Font("Courier New", Font.BOLD, 13));
            label21.setFont(new Font("Courier New", Font.BOLD, 13));
            label31.setFont(new Font("Courier New", Font.BOLD, 13));
            label41.setFont(new Font("Courier New", Font.BOLD, 13));
            label51.setFont(new Font("Courier New", Font.BOLD, 13));
            label61.setFont(new Font("Courier New", Font.BOLD, 13));
            JPanel panel1 = new JPanel();
            panel1.setLayout(new GridLayout(6, 1));
            panel1.add(label11);
            panel1.add(label21);
            panel1.add(label31);
            panel1.add(label41);
            panel1.add(label51);
            panel1.add(label61);
            panel1.setBackground(Color.WHITE);
            return panel1;
        }
    
        public static void main(String args[]) {
            UIManager.put("swing.boldMetal", Boolean.FALSE);
            JFrame f = new JFrame("Print UI Example");
    
            JButton printButton = new JButton("Print This Window");
    
            JPanel toPrint = makePanel();
            System.out.println(toPrint.getPreferredSize());
            forceLayout(toPrint);
            System.out.println(toPrint.getPreferredSize());
    
            printButton.addActionListener(new PrintUIWindow(toPrint));
            JPanel panel = makePanel();
            f.add(panel, BorderLayout.CENTER);
            f.add(printButton, BorderLayout.SOUTH);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    
            System.out.println(panel.getPreferredSize());
            System.out.println(panel.getSize());
        }
    }