📄 printabletextarea.java
字号:
//MenuFont version 1.0 alpha1//09/12/2001//author countryman@263.netimport java.awt.*;import java.awt.event.*;import java.io.*;import java.awt.print.*;import javax.swing.*;import javax.swing.text.*;class PrintableTextArea extends JTextArea implements Printable { String str = null; Font font = null; PrinterJob pj = null; PageFormat defaultFormat = null; int visiblePageWidth = 0, visiblePageHeight = 0; final static int PageX = 10, PageY = 10; PrintableTextArea(int rows, int columns) { super(rows, columns); } public void printIt(String str, Font font) { this.str = str; this.font = font; pj = PrinterJob.getPrinterJob(); defaultFormat = pj.defaultPage(); pj.setPrintable(this, defaultFormat); visiblePageWidth = (int)defaultFormat.getImageableWidth(); visiblePageHeight = (int)defaultFormat.getImageableHeight(); if(pj.printDialog()) { try { pj.print(); } catch(PrinterException e) { } } } //todo... use java.awt.print.Book instead of page print //only the first page to be printed now. public int print(Graphics g, PageFormat pf, int page) throws PrinterException { if(page >= 1) return Printable.NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) g; g2.translate((int)pf.getImageableX(), (int)pf.getImageableY()); g2.setFont(font); int fontHeight = 0, fontStringWidth = 0; int line = 0; String s = null; FontMetrics fm = getFontMetrics(font); fontHeight = fm.getHeight(); try{ DataOutputStream osw = new DataOutputStream(new FileOutputStream("$temp.$$p")); osw.writeBytes(str); osw.close(); } catch(IOException e) { } try { BufferedReader br = new BufferedReader(new FileReader("$temp.$$p")); s = br.readLine(); while(s != null) { if(s.length() == 0) g2.drawString(" ", PageX, PageY + fontHeight*(line++)); else { fontStringWidth = fm.stringWidth(s); if(fontStringWidth > visiblePageWidth) { String s1, s2; int goBack = 0; while( fm.stringWidth(s1 = s.substring(0, s.length() - goBack)) > visiblePageWidth) { goBack++; } s1 = s.substring(0, s.length() - goBack); s2 = s.substring(s.length() - goBack - 1); g2.drawString(s1, PageX, PageY + fontHeight*(line++)); g2.drawString(s2, PageX, PageY + fontHeight*(line++)); } else { g2.drawString(s, PageX, PageY + fontHeight*(line++)); } } s = br.readLine(); } br.close(); } catch(IOException e) { } return Printable.PAGE_EXISTS; } public static void main(String[] args) { JFrame jfr = new JFrame(); final PrintableTextArea ta = new PrintableTextArea(10,10); jfr.getContentPane().add("Center", ta); JButton jb = new JButton("Print"); jfr.getContentPane().add("South", jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, " 请连接好打印机.", "Print", JOptionPane.INFORMATION_MESSAGE); ta.printIt(ta.getText(), ta.getFont()); } }); jfr.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); jfr.setSize(320,240); jfr.show(); }}// end of class PrintableTextArea
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -