java
  • swing
  • printing
  • 2010-11-16 106 views 2 likes 
    2

    (我改写了原来的问题,问题是一样的。)从JEditorPane打印

    上面的例子代码不打印图像。它出现在窗口中但未打印。

    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); 
        } 
    

    回答

    4

    现在得到这个。 “秘密”是拦截图像加载过程并指示应该同步加载图像。

    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; 
            } 
           }; 
          } 
         }); 
    
    +0

    非常感谢! – ruX 2013-05-22 17:57:22

    +0

    Java有View,ImageView?我知道它来自Android ... – barwnikk 2013-11-18 00:48:38

    0

    试试这个:

    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

    这不是我真正想要的。我想通过代码设置页面内容(请参阅我编辑的问题)。 – PeterMmm 2010-11-17 14:44:32

    相关问题