将字符串分成段落,以便我可以在另一个类中打印它

时间:2016-10-24 01:58:31

标签: java printing

好的,所以我有一个代码从一个类获取信息,并在执行操作后将其发送到另一个类中打印。

此代码获取TextAreas中可用的字符串并合并它们以便将它们发送到其他类。这是ActionListener合并TextAreas并打开另一个框架以确认打印操作。

JButton button_2 = new JButton("Print  ");
    button_2.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            String line= textArea.getText();
            String line2=textArea_1.getText();
            String line3=line+line2;
             Print.setLine(line3);
                contentPane.setVisible(false);
                dispose();
                Print p=new Print();
                p.setVisible(true); 
        }

    });

这是类代码。它接收应该打印的String。唯一的问题是它应该打印成一个段落,并打印成一条直线(因为它是一个字符串)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


import java.awt.print.*;

public class Print implements Printable, ActionListener {
public static String line="";




public String getLine(){
    return line;
}
public static void setLine(String x){
    line=x;

}


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());   
    g.drawString(this.linea, 100, 100);
    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) {

         }
     }
}

   public static void main(String args[]) {

}



public void setVisible(boolean b) {
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    JFrame f = new JFrame("Print");
    f.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e) {System.exit(0);}
    });
    JButton printButton = new JButton("Click to print");
    printButton.addActionListener(new Print());
    f.add("Center", printButton);
    f.pack();
    f.setVisible(true);


}



}

如何将长字符串分成段落?如果我生成Word文档会更好吗?

0 个答案:

没有答案