gpt4 book ai didi

java - JTextPane 中没有滚动条

转载 作者:行者123 更新时间:2023-12-02 13:42:54 36 4
gpt4 key购买 nike

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

these is the image of output i am getting.

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);

关于java - JTextPane 中没有滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42630550/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com