回答
许多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秒钟就会感到惊讶,并可能真正回答自己的问题 - 对不起,但这太令人讨厌了 – MadProgrammer
我真的很抱歉,我很抱歉。你的回答非常有帮助,再次感谢你给了这么多时间。有一个美好的一天先生:) –
- 1. 打印JTextArea
- 2. 打印全部内容
- 3. 在Android中使用蓝牙打印机打印内容
- 4. 如何在php中使用打印机打印HTML内容?
- 5. 在完成打印div内容后停止打印
- 6. 打印内容
- 7. 打印内容
- 8. Div内容未在打印中显示
- 9. PHP - 打印内容到本地打印机在网络
- 10. 在JtextArea或JtextPane上打印
- 11. 在打印前预览JTextArea
- 12. 如何在JTextArea上打印
- 13. 使用PrintDocument打印到收据打印机不会打印所有内容
- 14. Sqlalchemy:打印内容
- 15. 打印内容DefaultTreeModel
- 16. 打印iframe内容
- 17. 打印NSMutableURLRequest内容
- 18. 打印ScrollViewer内容
- 19. 将嵌套div的内容打印到打印机
- 20. 如何使用打印机打印JFrame的内容
- 21. 将jQuery对话框的内容打印到打印机
- 22. 通过Zebra RW420打印机打印网页内容
- 23. 如何使用蓝牙打印机打印WebView内容
- 24. 设置为使用热敏打印机打印JFrame内容
- 25. 在PHP中打印到打印机
- 26. 在ZEBRA标签打印机中打印
- 27. 在爱普生打印机中打印
- 28. 发送JTextArea组件到打印机
- 29. 在打开Fancybox时打印内容,在关闭时打印页面内容
- 30. 打印到未在本机上定义的打印机
那么问题是什么? –
如果在像JEditorPane这样的样式化文档中使用HTML,可以很容易地将表中的项目与标题对齐,并将其表格单元格中的金额对齐。 –
因此,存在['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