jtextArea打印2个空白页

时间:2018-04-13 14:19:52

标签: java printing awt

我有一个jTextArea,它填充了程序发出的信息,但是,当我调用.print()时;方法,它打印2个空白页面,它显示弹出窗口和互联网上显示的其他信息,我认为它确实"见"文本区域中的数据因为它要打印两页,但问题是页面只是空白,任何想法我做错了什么?

try {
      boolean complete = txaMainOutput.print();
        if(complete)
        {
            JOptionPane.showMessageDialog(null, "Done Printing", "Information", JOptionPane.INFORMATION_MESSAGE);
        } else
        {
            JOptionPane.showMessageDialog(null, "Printing", "Printer", JOptionPane.ERROR_MESSAGE);
        }
    } catch (PrinterException ex) {
        JOptionPane.showMessageDialog(null, "An Error has occured, looks like we could not print");
    }

https://gyazo.com/376958811a5fd9c5356843d8bf83c36f

1 个答案:

答案 0 :(得分:0)

在玩完代码后,我发现创建一个新的JTextArea,将所有Origianal TextArea数据保存为字符串,然后将新的TextArea设置为该字符串,然后打印它将解决空白页面的问题。

        String YourString = "Line 1 \nLine 2 \nLine 3" //define and set contents of your string - remember about formating with \n if you want to have it split on lines
        JTextArea YourTextArea = new JTextArea(); //define new Swing JtextArea
        YourTextArea.setLineWrap(true); //set line wrap for YourTextArea - this will prevent too long lines to be cut - they will be wrapaed to next line
        YourTextArea.append(YourString); //append YourString to YourTextArea
        YourTextArea.print(); //this will display print dialog that will lead you to print contents of YourTextArea so in efect contents of YourString