【问题标题】:JTextArea content is not printing in printer fully [closed]JTextArea 内容未在打印机中完全打印 [关闭]
【发布时间】:2017-07-20 09:12:18
【问题描述】:

文本区域中显示的内容。

以及印刷品中的内容。

【问题讨论】:

  • 那么问题是什么?
  • 如果在JEditorPane..这样的样式文档中使用HTML,则可以很容易地将表格中的项目与标题对齐,并将表格单元格中的金额右对齐。
  • 如果你真的需要,可以使用JTextArea#getPrintable 或者直接使用 Jasper Reports
  • “以及印刷品中的内容。” - 了解印刷品的“方式”将对我们帮助您的能力产生巨大影响,我怀疑您只是打电话paint(Graphics)print(Graphics) 在您自己的 Printable 中,这将导致无穷无尽的问题

标签: java swing printing jtextarea


【解决方案1】:

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

    }
}

哪个可以输出...

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

【讨论】:

  • 非常感谢:) 但是我怎样才能打印标题?
  • 试过阅读 API 文档吗?至少有三种不同的打印方法,如果您花 5 秒时间,您可能会感到惊讶,并且实际上可能会回答您自己的问题 - 抱歉,但这非常烦人
  • 我真的很抱歉,我很抱歉。您的回答真的很有帮助,再次感谢您抽出这么多时间。祝你有美好的一天先生:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多