(我重写了原来的问题。问题是一样的。)
上面的示例代码不打印图像。它出现在窗口中但未打印。
public static void main(String[] args) {
final JEditorPane ed = new JEditorPane(
"text/html",
"<p>Test<br><img src='http://www.google.es/images/logos/ps_logo2.png'></p>");
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.add(ed);
JButton b = new JButton("Print");
f.add(b,BorderLayout.SOUTH);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
ed.print();
} catch (PrinterException ex) {
System.err.println(ex);
}
}
});
f.pack();
f.setVisible(true);
}
最佳答案
现在明白了。 “ secret ”是拦截图片加载过程并指示图片应该同步加载。
ed.setEditorKit(new HTMLEditorKit() {
@Override
public ViewFactory getViewFactory() {
return new HTMLFactory() {
@Override
public View create(Element elem) {
View view = super.create(elem);
if (view instanceof ImageView) {
((ImageView) view).setLoadsSynchronously(true);
}
return view;
}
};
}
});
关于java - 从 JEditorPane 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4197669/