2017-03-01 11 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コンポーネントは

...あなたは、たとえば、出力することができます

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(); 
        } 
       } 
      }); 
     } 

    } 
} 

ために、始めるためにJTextArea.printような単純なものを使用することができますボックス

のプリントアウトをサポート

enter image description here

標準以外のページサイズに印刷する必要がある場合は、How can I print a custom paper size (cheques 8" x 4")?をご覧ください。

+0

ありがとうございます:)しかし、どうすれば見出しを印刷できますか? –

+0

APIのドキュメントを読んでみましたか?少なくとも3つの異なる印刷方法があります.5秒かかると驚いて実際に自分の質問に答えるかもしれません - 残念ですが、それは非常に面倒です – MadProgrammer

+0

本当にすみません、お詫び申し上げます。あなたの答えは本当に役に立つものでしたし、もうあまりにも多くの時間を与えてくれてありがとう。良い一日を持っている:) –

関連する問題