我在我的 JTextPanel 中显示 RSS 提要。显示的结果不会换行。如何在 JTextPane 中插入 '\n'?谢谢!
writeNews 类:
public String writeNews()
{
String result = "";
try
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL("http://thestar.com.my.feedsportal.com/c/33048/f/534600/index.rss");
Document doc = builder.parse(u.openStream());
NodeList nodes = doc.getElementsByTagName("item");
for(int i=0;i<nodes.getLength();i++)
{
Element element = (Element)nodes.item(i);
result += "\nTitle: " + getElementValue(element,"title");
result += "\nLink: " + getElementValue(element,"link");
result += "\nPublish Date: " + getElementValue(element,"pubDate");
result += "\nDescription: " + getElementValue(element,"description");
System.out.println("Title: " + getElementValue(element,"title"));
System.out.println("Link: " + getElementValue(element,"link"));
System.out.println("Publish Date: " + getElementValue(element,"pubDate"));
System.out.println("Description: " + getElementValue(element,"description"));
System.out.println();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
return result;
}
结果显示在一个简单的 JTextPane 上:
public void news()
{
news = new JPanel(new GridLayout());
news.setBackground(SystemColor.inactiveCaptionBorder);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(newsTextPane);
news.add(scrollPane);
TextSamplerDemo reader = TextSamplerDemo.getInstance();
reader.writeNews();
String rssNews = reader.writeNews();
newsTextPane.setText(rssNews);
}
JTextPane 视图:
控制台视图: