为什么 JTextPane 中的超链接(一个标签)保持在中心(中心对齐)

我已经实现了两种方法,以便在JTextPane 中添加文本和超链接。问题是超链接显示为居中对齐,而不是文本(我希望两者都左对齐)。


方法是:

public void addText (String log, Color c, boolean bold) {

    StyledDocument doc = jTextPane.getStyledDocument();


    SimpleAttributeSet attrs = new SimpleAttributeSet();

    StyleConstants.setFontFamily(attrs, "Calibri");

    StyleConstants.setFontSize(attrs, 13);

    StyleConstants.setForeground(attrs, c);

    StyleConstants.setBold(attrs, bold);

    try {

        doc.insertString(doc.getLength(), log, attrs);

    } catch (BadLocationException ex) {

        Logger.getLogger(FrameLog.class.getName()).log(Level.SEVERE, null, ex);

    }

}


public void addHyperlink (URL url, String text) {

    StyledDocument doc = jTextPane.getStyledDocument();


    SimpleAttributeSet hrefAttr = new SimpleAttributeSet();

    hrefAttr.addAttribute(HTML.Attribute.HREF, url.toString());


    SimpleAttributeSet attrs = new SimpleAttributeSet();

    attrs.addAttribute(HTML.Tag.A, hrefAttr);


    StyleConstants.setFontFamily(attrs, "Calibri");

    StyleConstants.setFontSize(attrs, 13);

    StyleConstants.setForeground(attrs, Color.blue);


    try {

        doc.insertString(doc.getLength(), text, attrs);

    } catch (BadLocationException e) {

        e.printStackTrace(System.err);

    }

}



结果是这样的:

http://img4.mukewang.com/619def900001419104980097.jpg

完整的问题演示代码片段在这里

有谁知道我该如何解决这个问题?


万千封印
浏览 161回答 2
2回答

长风秋雁

我用一个泛型替换了我以前的方法,它将一些 html 代码附加到 JTextPane 中。新方法是:&nbsp; &nbsp; public void appendTextPane (String html) {&nbsp; &nbsp; &nbsp; &nbsp; HTMLEditorKit editor = (HTMLEditorKit) jTextPane.getEditorKit();&nbsp; &nbsp; &nbsp; &nbsp; HTMLDocument doc = (HTMLDocument) jTextPane.getDocument();&nbsp; &nbsp; &nbsp; &nbsp; String TAG;&nbsp; &nbsp; &nbsp; &nbsp; if (html.charAt(0) != '<') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TAG = "";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextSpaceIdx = html.indexOf(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextBrcktIdx = html.indexOf(">");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TAG = html.substring(1, nextBrcktIdx < nextSpaceIdx ? nextBrcktIdx : nextSpaceIdx);&nbsp; &nbsp; &nbsp; &nbsp; }//&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jTextPane.getText());&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.insertHTML(doc, doc.getLength(), html.replaceAll(" ", "&#160;"), 0, 0, HTML.getTag(TAG));&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException | BadLocationException ex) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }固定预览是这样的:完整的解决方案演示代码片段:import java.awt.*;import java.io.IOException;import java.net.URISyntaxException;import javax.swing.*;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.text.*;import javax.swing.text.html.HTML;import javax.swing.text.html.HTMLDocument;import javax.swing.text.html.HTMLEditorKit;public class SolutionDemo {&nbsp; &nbsp; private JFrame jFrame;&nbsp; &nbsp; private JTextPane jTextPane;&nbsp; &nbsp; private JScrollPane jScrollPane;&nbsp; &nbsp; public SolutionDemo () {&nbsp; &nbsp; &nbsp; &nbsp; //CREATE THE COMPONENTS AND SHOW THE FRAME WINDOW&nbsp; &nbsp; &nbsp; &nbsp; jFrame = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp; jTextPane = new JTextPane();&nbsp; &nbsp; &nbsp; &nbsp; jScrollPane = new JScrollPane(jTextPane);&nbsp; &nbsp; &nbsp; &nbsp; jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);&nbsp; &nbsp; &nbsp; &nbsp; jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);&nbsp; &nbsp; &nbsp; &nbsp; jFrame.setLayout(new FlowLayout());&nbsp; &nbsp; &nbsp; &nbsp; //I NEED IT TO BE HTML SO THE HYPERLINK TO BE CLICKABLE&nbsp; &nbsp; &nbsp; &nbsp; jTextPane.setContentType("text/html");&nbsp; &nbsp; &nbsp; &nbsp; jTextPane.setEditable(false);&nbsp; &nbsp; &nbsp; &nbsp; jTextPane.setPreferredSize(new Dimension(600, 100));&nbsp; &nbsp; &nbsp; &nbsp; jFrame.add(jScrollPane);&nbsp; &nbsp; &nbsp; &nbsp; jFrame.pack();&nbsp; &nbsp; &nbsp; &nbsp; jFrame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; jFrame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; //ADD LISTENER IN ORDER TO OPEN THE LINK ON BROWSER WHEN CLICKED&nbsp; &nbsp; &nbsp; &nbsp; jTextPane.addHyperlinkListener(new HyperlinkListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void hyperlinkUpdate (HyperlinkEvent evt) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Desktop.isDesktopSupported()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Desktop.getDesktop().browse(evt.getURL().toURI());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException | URISyntaxException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ex.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; public void init () {&nbsp; &nbsp; &nbsp; &nbsp; //SET GLOBAL STYLES&nbsp; &nbsp; &nbsp; &nbsp; jTextPane.setText(""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "<head>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " <style type=\"text/css\">"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;body{ "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;color: green;"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;font-family: calibri"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "&nbsp; &nbsp; &nbsp;}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " </style>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "</head>");&nbsp; &nbsp; &nbsp; &nbsp; //ADD SOME TEXT AND HYPERLINKS&nbsp; &nbsp; &nbsp; &nbsp; String url = "https://www.example.com";&nbsp; &nbsp; &nbsp; &nbsp; appendTextPane("No tag must be inserted first in order to custom header work with this impl ");&nbsp; &nbsp; &nbsp; &nbsp; appendTextPane("<span>BLAH blah BLAH blah BLAH blah BLAH blah </span>");&nbsp; &nbsp; &nbsp; &nbsp; appendTextPane("<span style=\"color:red\">BLAH blah BLAH blah BLAH blah BLAH blah </span>");&nbsp; &nbsp; &nbsp; &nbsp; appendTextPane("<a href=\"" + url + "\">" + url + "</a>");&nbsp; &nbsp; &nbsp; &nbsp; appendTextPane("<span> BLAH blah BLAH blah BLAH blah BLAH blah <a href=\"" + url + "\">" + url + "</a></span>");&nbsp; &nbsp; &nbsp; &nbsp; appendTextPane("With no tag the text goes to next line <a href=\"" + url + "\">" + url + "</a>");&nbsp; &nbsp; }&nbsp; &nbsp; public void appendTextPane (String html) {&nbsp; &nbsp; &nbsp; &nbsp; HTMLEditorKit editor = (HTMLEditorKit) jTextPane.getEditorKit();&nbsp; &nbsp; &nbsp; &nbsp; HTMLDocument doc = (HTMLDocument) jTextPane.getDocument();&nbsp; &nbsp; &nbsp; &nbsp; String TAG;&nbsp; &nbsp; &nbsp; &nbsp; if (html.charAt(0) != '<') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TAG = "";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextSpaceIdx = html.indexOf(" ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nextBrcktIdx = html.indexOf(">");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TAG = html.substring(1, nextBrcktIdx < nextSpaceIdx ? nextBrcktIdx : nextSpaceIdx);&nbsp; &nbsp; &nbsp; &nbsp; }//&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(jTextPane.getText());&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //REPLACE SPACES WITH ITS NUMERIC ENTITY REFERENCE IN ORDER TO SHOW ALL THE EXISTING SPACES&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.insertHTML(doc, doc.getLength(), html.replaceAll(" ", "&#160;"), 0, 0, HTML.getTag(TAG));&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException | BadLocationException ex) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main (String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; new SolutionDemo().init();&nbsp; &nbsp; }}谢谢camickr!不幸的是,由于缺乏声誉,我无法为您的答案投票。

猛跑小猪

值得一提的是,当我尝试操作文档中的 HTML 文本时,我有一些旧的(丑陋的)代码。我使用了除insertString(...). 也许这会给你一些想法?import java.awt.*;import java.io.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;import javax.swing.text.html.*;public class TextPaneHTML extends JFrame implements ActionListener{&nbsp; &nbsp; JTextPane textPane;&nbsp; &nbsp; HTMLEditorKit editorKit;&nbsp; &nbsp; HTMLDocument doc;&nbsp; &nbsp; Element root;&nbsp; &nbsp; public TextPaneHTML()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; textPane = new JTextPane();//&nbsp; &nbsp; &nbsp; textPane.setEditable( false );&nbsp; &nbsp; &nbsp; &nbsp; textPane.setContentType( "text/html" );&nbsp; &nbsp; &nbsp; &nbsp; textPane.setEditable(false);&nbsp; &nbsp; &nbsp; &nbsp; editorKit = (HTMLEditorKit)textPane.getEditorKit();&nbsp; &nbsp; &nbsp; &nbsp; doc = (HTMLDocument)textPane.getDocument();&nbsp; &nbsp; &nbsp; &nbsp; root = doc.getDefaultRootElement();&nbsp; &nbsp; &nbsp; &nbsp; DefaultCaret caret = (DefaultCaret)textPane.getCaret();&nbsp; &nbsp; &nbsp; &nbsp; caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);&nbsp; &nbsp; &nbsp; &nbsp; textPane.setText( "<html><body>Hello world I want some long text <a href=\"http://answers.polldaddy.com/poll/1742928/\">Do you like polls</a> to it wraps to a new line World!</body></html>" );//&nbsp; &nbsp; &nbsp; textPane.setCaretPosition(7);//&nbsp; &nbsp; &nbsp; textPane.setText( "" );//&nbsp; &nbsp; &nbsp; printInfo();&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; Add text pane to frame&nbsp; &nbsp; &nbsp; &nbsp; JScrollPane scrollPane = new JScrollPane( textPane );&nbsp; &nbsp; &nbsp; &nbsp; scrollPane.setPreferredSize( new Dimension( 200, 200 ) );&nbsp; &nbsp; &nbsp; &nbsp; getContentPane().add( scrollPane );&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; Add a append button&nbsp; &nbsp; &nbsp; &nbsp; JPanel buttonPanel = new JPanel();&nbsp; &nbsp; &nbsp; &nbsp; buttonPanel.setLayout( new GridLayout(1, 0) );&nbsp; &nbsp; &nbsp; &nbsp; getContentPane().add( buttonPanel, BorderLayout.SOUTH );&nbsp; &nbsp; &nbsp; &nbsp; createButton("Normal", buttonPanel);&nbsp; &nbsp; &nbsp; &nbsp; createButton("Styled", buttonPanel);&nbsp; &nbsp; &nbsp; &nbsp; createButton("B1", buttonPanel);&nbsp; &nbsp; &nbsp; &nbsp; createButton("B2", buttonPanel);&nbsp; &nbsp; &nbsp; &nbsp; createButton("B3", buttonPanel);&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);&nbsp; &nbsp; &nbsp; &nbsp; while (it.isValid())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SimpleAttributeSet s = (SimpleAttributeSet)it.getAttributes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(s);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.removeAttribute(HTML.Attribute.HREF);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( s.getClass().getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String href = (String)s.getAttribute(HTML.Attribute.HREF);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int start = it.getStartOffset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int end = it.getEndOffset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String text = doc.getText(start, end - start);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( href + " : " + text );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; it.next();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e) {};&nbsp; &nbsp; }&nbsp; &nbsp; public void createButton(String text, JPanel panel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; JButton button = new JButton(text);&nbsp; &nbsp; &nbsp; &nbsp; button.addActionListener( this );&nbsp; &nbsp; &nbsp; &nbsp; panel.add( button );&nbsp; &nbsp; }&nbsp; &nbsp; public void actionPerformed(ActionEvent ae)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String command = ae.getActionCommand();&nbsp; &nbsp; &nbsp; &nbsp; String text;&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("Normal".equals( command ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "normal text";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("Styled".equals( command ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<font size=5 color=red>font and color test</font>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<a href=\"abc\">hyperlink</a>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editorKit.insertHTML(doc, doc.getLength(), text, 0, 0, null);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.insertString(doc.getLength(), text, null);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editorKit.read(new StringReader(text), doc, doc.getLength());//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("B1".equals( command ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<br>B1";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<a href=\"abc\">hyperlink</a>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Element element = root.getElement(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.insertBeforeEnd(element, text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("B2".equals( command ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Element element = root.getElement(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<br>B2-after start";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.insertAfterStart(element, text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // why doesn't this work&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<br>B2-before end";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.insertBeforeEnd(element, text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ("B3".equals( command ))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<p>B3</p>";//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.P);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text = "<a href=\"abc\">hyperlink</a>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editorKit.insertHTML(doc, textPane.getCaretPosition(), text, 0, 0, HTML.Tag.A);//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.insertString(doc.getLength(), text, null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BadLocationException ble = (BadLocationException)e;//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e + " : " + ble.offsetRequested());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(e);&nbsp; &nbsp; &nbsp; &nbsp; }//&nbsp; &nbsp; &nbsp; System.out.println( command + ": -----------" );//&nbsp; &nbsp; &nbsp; System.out.println( "Elements: " + root.getElementCount() );//&nbsp; &nbsp; &nbsp; System.out.println( root.getElement(0).getAttributes() );//&nbsp; &nbsp; &nbsp; System.out.println( root.getElement(1).getAttributes() );&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "textPane: " + textPane.getText() );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( "Document: " + textPane.getDocument().getText(0, textPane.getDocument().getLength()) );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(Exception e2) {}&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; EventQueue.invokeLater(new Runnable()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; TextPaneHTML frame = new TextPaneHTML();&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation( EXIT_ON_CLOSE );&nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(50, 120);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java