我需要显示链接,所以我正在使用带有setContentType的JTextPane。但是,内容不会换行,也没有滚动。 JTextPane的内容将从RSS源返回。这是完整的代码:
import java.awt.*;
import javax.swing.*;
class Main extends JFrame
{
JFrame frame;
JTabbedPane tabbedPane;
JPanel home, news;
public Main()
{
setTitle("My Title" );
setSize( 900, 600 );
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
home();
news();
tabbedPane = new JTabbedPane();
tabbedPane.addTab( " Home", home );
tabbedPane.addTab( "News", news );
JPanel framePanel = new JPanel();
framePanel.setLayout(new BorderLayout());
framePanel.add( tabbedPane, BorderLayout.CENTER );
getContentPane().add( framePanel );
}
public void home()
{
home = new JPanel();
// some stuffs here
}
public void news()
{
news = new JPanel();
JTextPane newsTextPane = new JTextPane();
newsTextPane.setContentType("text/html");
newsTextPane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(newsTextPane);
scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
news.add(scrollPane);
RSS reader = RSS .getInstance();
reader.writeNews();
String rssNews = reader.writeNews();
newsTextPane.setText(rssNews);
}
public static void main( String args[] )
{
RSS reader = RSS.getInstance();
reader.writeNews();
Main mainFrame = new Main();
mainFrame.setVisible( true );
mainFrame.setDefaultCloseOperation( EXIT_ON_CLOSE );
}
}
我的结果:
答案 0 :(得分:6)
我刚刚使用了你的代码,它不会导致任何问题:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities
public class TestScrolling {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initUI();
});
}
public static void initUI() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append("loads loads loads loads of text here ");
}
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);
scrollPane.setVerticalScrollBarPolicy(
javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
frame.add(scrollPane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
编辑:
你必须以某种方式强制scrollPane的宽度。在我的示例中,它通过将滚动窗格添加到框架的内容窗格来隐式完成,默认情况下使用BorderLayout。在您的情况下,您使用了一个FlowLayout,它分配了滚动窗格的首选大小,该大小与JTextPane的首选大小有关。
答案 1 :(得分:0)
您是否在JScrollPane周围使用面板或其他内容?
将@Guillaume Polet的sscc设置为不合适的大小,示例将不起作用:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestScrolling {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100; i++) {
sb.append("loads loads loads loads of text here ");
}
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);
scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
JPanel pan = new JPanel();
pan.setMinimumSize(new Dimension(500,500));
pan.add(scrollPane);
frame.add(pan);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
我看到您将JscrollPane添加到panel
。您能否提供您在该面板上创建/修改的内容以及使用此面板的位置?