2017-03-01 81 views
-5

正文出現在文本區域中。JTextArea內容未在打印機中完全打印

什麼是在打印。

+0

那麼問題是什麼? –

+0

如果在像JEditorPane這樣的樣式化文檔中使用HTML,可以很容易地將表中的項目與標題對齊,並將其表格單元格中的金額對齊。 –

+1

因此,存在['JTextArea#print'] (https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print()),['JTextArea#print(MessageFormat,MessageFormat)'](https:/ /docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print(java.text.MessageFormat,%20java.text.MessageFormat)),['JTextArea#print(lots of參數)'](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#print(java.text.MessageFormat中,%20java.text.MessageFormat,%20boolean ,%20javax.print.PrintService,%20javax.print.attribute.PrintRequestAttributeSet,%20boolean)) – MadProgrammer

回答

2

許多Swing組件支持打印開箱

你可以使用作爲JTextArea.print一樣簡單上手,例如

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Graphics2D; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Rectangle2D; 
import java.awt.image.BufferedImage; 
import java.awt.print.PageFormat; 
import java.awt.print.Paper; 
import java.awt.print.Printable; 
import java.awt.print.PrinterException; 
import java.io.File; 
import java.io.IOException; 
import java.util.StringJoiner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.imageio.ImageIO; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 
import javax.swing.border.LineBorder; 

public class Test { 

    public static void main(String[] args) { 
     new Test(); 
    } 

    public Test() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new TestPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class TestPane extends JPanel { 

     private JTextArea ta; 

     public TestPane() { 
      setLayout(new BorderLayout()); 
      String[] lines = { 
       "Idx  Met  MTU  State    Name   ", 
       "--- --------- ---------- ------------ --------------------------", 
       " 1   50 4294967295 connected  Loopback Psudo-Interface 1", 
       " 11   10  1500 connected  Local Area Connection  ", 
       " 11   5  1500 disconnected Local Area Connection 3 ",}; 
      StringJoiner joiner = new StringJoiner("\n"); 
      for (String line : lines) { 
       joiner.add(line); 
      } 
      ta = new JTextArea(joiner.toString()); 
      ta.setBorder(new LineBorder(Color.RED)); 
      ta.setFont(new Font("Monospaced", Font.PLAIN, 13)); 
      ta.setWrapStyleWord(true); 
      add(new JScrollPane(ta)); 

      JButton btn = new JButton("Print"); 
      add(btn, BorderLayout.SOUTH); 

      btn.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        try { 
         ta.print(); 
        } catch (PrinterException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      }); 
     } 

    } 
} 

可以輸出...

enter image description here

如果您需要打印到非標準頁面尺寸,您可能還想看看How can I print a custom paper size (cheques 8" x 4")?

+0

非常感謝:)但是我怎樣才能打印標題? –

+0

甚至嘗試閱讀API文檔?至少有三種不同的打印方式,也許如果你花5秒鐘就會感到驚訝,並可能真正回答自己的問題 - 對不起,但這太令人討厭了 – MadProgrammer

+0

我真的很抱歉,我很抱歉。你的回答非常有幫助,再次感謝你給了這麼多時間。有一個美好的一天先生:) –