问题
How would I go on creating a horizontal line element in JTextPane? Just a rectangle painted probably by View.paint(Graphics), which covers the whole width of the JTextPane, and has an arbitrary height. I also need to be able to dynamically remove these elements.
I'm attempting to approach this by creating a custom editor kit, which has a custom view factory, which returns a "HorizontalLineView" for line elements... but I must admit that this all is a bit over my head! How do I, for example, create the element for this HorizontalLineView? I've only used insertString() so far... and I don't see "addElement" method anywhere... any pointers to right direction would be great.
I'll sneak in another question: Can anyone recommend a good book that covers JEditorPane/JTextPane in depth?
回答1:
If HTML content is acceptable, you could do something like:
JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText("<html>Some Text Above The Line<hr size=5>Some Text Below</html>");
回答2:
Or when you need "styled hr" you need to work around with div.
JEditorPane supports only HTML 2.1 and attributes for hr in html 2.1 are noshade and size (not style).
so for more fancy looking or 1px sized hr use this...
<div style=\"height: 1px; font-size:0; background:blue;\"></div>
or
<div style='height: 1px; font-size:0; background:blue;'></div>
where background is hr color and height is hr size. the font-size:0 attribute is important otherwise JEditor pane uses text wrap by default and this will override height attribute.
来源:https://stackoverflow.com/questions/12787121/jtextpane-and-horizontal-lines