gpt4 book ai didi

java - 为什么 textPane.getDocument().getText() 返回空字符串?

转载 作者:行者123 更新时间:2023-12-01 22:16:43 27 4
gpt4 key购买 nike

尝试从内容类型为“text/html”的 JTextPane 组件获取文本(在其位置上带有换行符)时,我做错了什么 ?

textPane.getText() 返回 HTML,但内容为空 headbodytextPane.getDocument().getText() 返回空字符串

P.S.:我在同一代码中也没有换行和其他问题,我是否应该使用相同的代码将它们作为独立问题提出?

代码:

package test;

import java.io.IOException;
import java.io.StringReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;
//import javax.swing.text.StyledEditorKit;

public class TextPaneTester extends javax.swing.JFrame {

public TextPaneTester() {
initComponents();
myInitComponents();
}

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
contentScrollPane = new javax.swing.JScrollPane();
content = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
contentScrollPane.setViewportView(content);

// layout code by NetBeans
}// </editor-fold>

private void myInitComponents(){
//content.setEditorKit(new StyledEditorKit());
contentScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
}

private void fetchURL(String url){
try{
// URL(URL baseURL[, String relativeURL])
URL helpURL = new URL(url);
System.out.println(helpURL);
this.content.setPage(helpURL);

try {
System.out.println("This far!");
//System.out.println(this.content.getDocument());
System.out.println("getDocument() on JTextPane gives " + this.content.getDocument());
System.out.println("getText() on JTextPane gives " + this.content.getText());

// Why this line retuens an empty string?
String text = this.content.getDocument().getText(0, this.content.getDocument().getLength());
System.out.println("getDocument.getText() on JTextPane gives " + text);

System.out.println("This far 2!");
// this.translation.setPage(helpURL);
} catch (BadLocationException ex) {
System.out.println("Why this far!");
Logger.getLogger(TextPaneTester.class.getName()).log(Level.SEVERE, null, ex);
}

}
catch (IOException e) {
System.err.println("Attempted to read a bad URL: " + url);
}
// return this.content;
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TextPaneTester.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
String url = "https://en.wikipedia.org/wiki/Virtaal";
TextPaneTester reader = new TextPaneTester();
reader.fetchURL(url);
reader.setVisible(true);
}
});
}

private javax.swing.JTextPane content;
private javax.swing.JScrollPane contentScrollPane;
// End of variables declaration
}

输出:

run:
https://en.wikipedia.org/wiki/Virtaal
This far!
getDocument() on JTextPane gives javax.swing.text.html.HTMLDocument@1767743f
getText() on JTextPane gives <html>
<head>

</head>
<body>
<p style="margin-top: 0">

</p>
</body>
</html>

getDocument.getText() on JTextPane gives
This far 2!
<html>
<head>

</head>
<body>
<p style="margin-top: 0">

</p>
</body>
</html>

Let's see what the parser gives:
BUILD SUCCESSFUL (total time: 55 seconds)

最佳答案

像往常一样,documentation是您应该查看的第一个地方:

If the document is loaded asynchronously, the document will be installed into the editor immediately using a call to setDocument which will fire a document property change event, then a thread will be created which will begin doing the actual loading. In this case, the page property change event will not be fired by the call to this method directly, but rather will be fired when the thread doing the loading has finished. It will also be fired on the event-dispatch thread.

您正在页面加载完成之前阅读文档。通过监视 page 属性等待其加载:

this.content.addPropertyChangeListener("page",
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent event) {
System.out.println("getText() on JTextPane gives "
+ content.getText());
}
});

this.content.setPage(helpURL);

关于java - 为什么 textPane.getDocument().getText() 返回空字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com