我没有得到垂直滚动条。滚动JTextPane
。
我正在使用JPanel
来显示JTextPane
内的JScrollPane
。
这是代码。请检讨一下。
感谢。
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();
}
}
答案 0 :(得分:1)
scrollpane = new JScrollPane(textpane);
panel.add(scrollpane, BorderLayout.CENTER);
textpane.setSize(size);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(textpane);
frame.add(panel);
使用正确的文本窗格创建滚动窗格。
但随后您将文本窗格添加到面板。这不起作用,因为组件只能有一个父组件,因此文本窗格将从滚动窗格中删除。
所以摆脱面板,只需将滚动窗格添加到框架中。
此外,不需要setSize(...),因为滚动窗格的布局管理器将确定文本窗格的大小。
所以你的基本代码应该是:
scrollpane = new JScrollPane(textpane);
scrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scrollpane);