gpt4 book ai didi

java - 将 JTextArea 组件发送到打印机

转载 作者:太空宇宙 更新时间:2023-11-04 06:27:02 25 4
gpt4 key购买 nike

我已经工作了 12 个小时,试图将一个简单的 jtextarea 组件的内容发送到打印机。疯了吧。我按照之前的代码中的建议查看了 JavaDocs,但坦率地说,该代码不起作用并读取错误。我非常沮丧,因为打印 JTable 的内容非常简单(大约 8 行代码),但由于某种原因,打印 jtextarea 的内容完全不同,而且要复杂得多,涉及多个类,在某些示例中超过 100 行代码。我不明白为什么简单地打印一个Java组件的过程会如此复杂。

谁能告诉我为什么下面的代码会读取错误“添加参数以匹配打印(图形)”以及如何修复它,以便我可以将文本区域组件发送到打印机并继续我的生活。

JButton btnNewButton_7 = new JButton("Print");
btnNewButton_7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {

try{
boolean complete = textArea_2.print();
//The above line reads the error "Add argument to match print(Graphics)"
if(complete){
JOptionPane.showMessageDialog(null, "Printjob Finished", "Report",
JOptionPane.INFORMATION_MESSAGE);
}else{
JOptionPane.showMessageDialog(null, "Printing", "Printer", JOptionPane.ERROR_MESSAGE);
}
}catch(PrinterException e){JOptionPane.showMessageDialog(null, e);
}
}

});

最佳答案

您可能想看看StandardPrint 。您可以删除引用 WindowUtilities 的行

来自该类(class):

    JFrame jf = new JFrame("StandardPrint Test");
final JTextArea area = new JTextArea();
area.append("hello\n");
for (int i = 0; i < 50; i++) {
area.append("\n");
}
area.append("world\n");
JScrollPane sp = new JScrollPane(area);
jf.add(sp);
JMenuBar bar = new JMenuBar();
JMenu printMenu = new JMenu("Print");
JMenuItem print = new JMenuItem("Print");
printMenu.add(print);
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
StandardPrint sp = new StandardPrint(area);
sp.setTitle("Hello World");
sp.setPrintPageNumber(true);
sp.setPageNumberVAlignment(BOTTOM);
sp.setPageNumberHAlignment(CENTER);
System.out.println(sp.getNumberOfPages());
Image im1 = preview(300,300, sp, sp.getPageFormat(0), 0);
Image im2 = preview(300,300, sp, sp.getPageFormat(1), 1);
JLabel l = new JLabel(new ImageIcon(im1));
JOptionPane.showMessageDialog(null, l);
l = new JLabel(new ImageIcon(im2));
JOptionPane.showMessageDialog(null, l);
}
};
print.addActionListener(al);
jf.setJMenuBar(bar);
bar.add(fm);
bar.add(printMenu);
jf.setBounds(100,100,400,400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}

关于java - 将 JTextArea 组件发送到打印机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26659321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com