我正在使用字符串構建器將文本附加到我的JTextPane,我已將內容類型設置爲pane.setContentType("text/html");
,但沒有文本實際出現在我的JTextPane上。將JTextPane設置爲內容類型HTML並使用字符串構建器
這是我追加的例子:
buildSomething.append("<b style=\"color:pink\">"+Birthday+"</span>");
有什麼我做的嚴重錯誤?我該如何解決它?
我正在使用字符串構建器將文本附加到我的JTextPane,我已將內容類型設置爲pane.setContentType("text/html");
,但沒有文本實際出現在我的JTextPane上。將JTextPane設置爲內容類型HTML並使用字符串構建器
這是我追加的例子:
buildSomething.append("<b style=\"color:pink\">"+Birthday+"</span>");
有什麼我做的嚴重錯誤?我該如何解決它?
每次調用JTextPane.setText(...)
時,都會確定新的內容類型。用"<html>"
開始文本,你已經有了HTML。
創建一個新文檔,在你的情況HTMLDocument。
@mKorbel:以下每次爲JTextPane創建HTML。
buildSomething.append("<html>");
buildSomething1.append("<html>");
for (int i = 0; i < 10; i++) {
buildSomething.append("<span style=\"color:red\">" + myBirthday + "</span>");
buildSomething1.append("<b style=\"color:blue\">" + myBirthday + "</b>");
}
@Joop埃根
1日。循環產生
buildSomething.append("<span style=\"color:pink\">" + myBirthday + "</span>");
第二。循環產生相同的輸出,我想是因爲有pane.setContentType("text/html");
和(不正確的代碼,我張貼在這裏<html> ..</html>
)如果包裹在裏面<html> ..<html>
與否不要緊
buildSomething1.append("<html><span style=\"color:pink\">"
+ myBirthday + "</span></html>");
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
public class MyTextPane implements Runnable {
private JFrame frm;
private JScrollPane jsp;
private JTextPane jta;
private StringBuilder buildSomething = new StringBuilder();
private StringBuilder buildSomething1 = new StringBuilder();
final String myBirthday = "Birthday";
public MyTextPane() {
for (int i = 0; i < 10; i++) {
buildSomething.append("<span style=\"color:red\">" + myBirthday + "</span>");
buildSomething1.append("<span style=\"color:blue\">" + myBirthday + "</span>");
}
jta = new JTextPane();
jta.setContentType("text/html");
jta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
jta.setText(myBirthday);
jsp = new JScrollPane(jta);
jsp.setPreferredSize(new Dimension(250, 450));
frm = new JFrame("awesome");
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setLayout(new BorderLayout());
frm.add(jsp, BorderLayout.CENTER);
frm.setLocation(100, 100);
frm.pack();
frm.setVisible(true);
new Thread(this).start();
}
@Override
public void run() {
try {
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jta.setText(null);
HTMLDocument doc = (HTMLDocument) jta.getStyledDocument();
try {
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), buildSomething.toString());
} catch (BadLocationException ex) {
Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
try {
Thread.sleep(1500);
} catch (Exception e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
HTMLDocument doc = (HTMLDocument) jta.getStyledDocument();
try {
doc.insertAfterEnd(doc.getCharacterElement(doc.getLength()), buildSomething1.toString());
} catch (BadLocationException ex) {
Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MyTextPane.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MyTextPane fs = new MyTextPane();
}
});
}
}
so'buildSomething.append(「」+ Birthday +「」);'?我試過了,但它仍然是一樣的。 – orange 2012-01-30 22:05:21
你做了'buildSomething = new StringBuilder(); buildSomething.append(「」+ ...); textPane.setText(buildSomething.toString());'? – 2012-01-30 22:30:45
爲好線程+1 – mKorbel 2012-01-31 07:23:37