首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从JEditorPane打印

从JEditorPane打印
EN

Stack Overflow用户
提问于 2010-11-17 02:33:36
回答 2查看 4.3K关注 0票数 2

(我重写原来的问题。问题是一样的。)

上面的示例代码不打印图像。它会出现在窗口中,但不会打印出来。

代码语言:javascript
复制
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);
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-18 19:14:58

现在明白了。“秘密”是截取镜像加载过程,并指示镜像应同步加载。

代码语言:javascript
复制
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;
                    }
                };
            }
        });
票数 5
EN

Stack Overflow用户

发布于 2010-11-17 03:08:42

试试这个:

代码语言:javascript
复制
import java.io.IOException;
import java.net.URL;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;

public class MainClass {

    public static void main(final String args[]) {
        final JFrame frame = new JFrame("EditorPane Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            final JEditorPane editorPane = new JEditorPane("http://www.google.com");
            editorPane.setEditable(false);

            final HyperlinkListener hyperlinkListener = new ActivatedHyperlinkListener(editorPane);
            editorPane.addHyperlinkListener(hyperlinkListener);

            final JScrollPane scrollPane = new JScrollPane(editorPane);
            frame.add(scrollPane);
        }
        catch (final IOException e) {
            System.err.println("Unable to load: " + e);
        }

        frame.setSize(640, 480);
        frame.setVisible(true);
    }

}

class ActivatedHyperlinkListener
    implements HyperlinkListener {

    JEditorPane editorPane;

    public ActivatedHyperlinkListener(final JEditorPane editorPane) {
        this.editorPane = editorPane;
    }

    public void hyperlinkUpdate(final HyperlinkEvent hyperlinkEvent) {
        final HyperlinkEvent.EventType type = hyperlinkEvent.getEventType();
        final URL url = hyperlinkEvent.getURL();
        if (type == HyperlinkEvent.EventType.ENTERED) {
            System.out.println("URL: " + url);
        }
        else if (type == HyperlinkEvent.EventType.ACTIVATED) {
            System.out.println("Activated");
            final Document doc = this.editorPane.getDocument();
            try {
                this.editorPane.setPage(url);
                this.editorPane.print();
            }
            catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4197669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档