IT虾米网

java之JTextPane 中没有滚动条

yxwkf 2024年01月16日 程序员 55 0

我没有得到垂直滚动条。滚动JTextPane。 我正在使用 JPanel 显示 JScrollPane 内部的 JTextPane。 这是代码。请查看。 谢谢。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
class TestWithHtml 
{ 
    JTextPane textpane; 
    JPanel panel; 
    JFrame frame; 
    JScrollPane scrollpane; 
 
    TestWithHtml() 
    { 
        frame = new JFrame("Text With HTML"); 
        frame.setSize(200,300); 
 
        panel = new JPanel(new BorderLayout()); 
        panel.setBounds(0,0,200,300); 
 
        textpane = new JTextPane(); 
        Dimension size = new Dimension(200,300); 
        //frame.getContentPane() 
        scrollpane = new JScrollPane(textpane); 
        panel.add(scrollpane, BorderLayout.CENTER); 
        textpane.setSize(size); 
        scrollpanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
 
        panel.add(textpane); 
        frame.add(panel); 
        textpane.setEditable(false); 
        textpane.setContentType("text/html"); 
        textpane.setText("<b> hry </b>"); 
        textpane.setText("<p><b>Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. Here Is some text. </b></p>"); 
 
        frame.setVisible(true); 
    } 
 
    public static void main(String args[]) 
    { 
        new TestWithHtml(); 
    } 
} 

请您参考如下方法:

scrollpane = new JScrollPane(textpane); 
panel.add(scrollpane, BorderLayout.CENTER); 
textpane.setSize(size); 
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
panel.add(textpane); 
frame.add(panel); 

您使用正确的文本 Pane 创建了滚动 Pane 。

但随后您将文本 Pane 添加到面板中。这不起作用,因为组件只能有一个父组件,因此文本 Pane 将从滚动 Pane 中删除。

因此,去掉面板,只需将滚动 Pane 添加到框架中即可。

此外,不需要 setSize(...),因为滚动 Pane 的布局管理器将确定文本 Pane 的大小。

所以你的基本代码应该是:

scrollpane = new JScrollPane(textpane); 
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 
frame.add(scrollpane); 


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!