本文介绍了JTextPane不会转到新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JTextPanel中显示RSS提要。显示的结果不会转到新行。我如何在JTextPane中插入\\\
?谢谢!
writeNews class:
$ p $
public String writeNews()
{
String result =;
尝试
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance()。newDocumentBuilder();
网址u =新网址(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 + =\\\
Title:+ getElementValue(element,title);
result + =\\\
Link:+ getElementValue(element,link);
result + =\ nPublish Date:+ getElementValue(element,pubDate);
result + =\\\
Description:+ 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();
$ b catch(Exception ex)
{
ex.printStackTrace();
}
返回结果;
}
结果显示在一个简单的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查看:
控制台视图:
解决方案
一个可能的解决方法,
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestLineBreak {
public static void main(String [] args){
StringBuilder sb = new StringBuilder();
for(int i = 0; i sb.append(Text goes here< br>); //<峰; br>标记插入换行符
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType(text / html);
newsTextPane.setEditable(false);
newsTextPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(newsTextPane);
frame.add(scrollPane);
frame.setSize(300,200);
frame.setVisible(true);
}
}
I'm displaying RSS feed in my JTextPanel. Result displayed doesn't go to a new line. How can I insert \n in JTextPane? Thanks!
writeNews class:
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;
}
And the result are displayed on a simple 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 View:
Console View:
解决方案
Here is a possible workaround,
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestLineBreak {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append("Text goes here <br>"); //<br> tag to insert line breaks
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
newsTextPane.setText(sb.toString());
JScrollPane scrollPane = new JScrollPane(newsTextPane);
frame.add(scrollPane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
这篇关于JTextPane不会转到新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!