我已经创建了一个文本区,我需要一个滚动条应用到文本区时(当文本变得太长,它不能再读了)。
这是我写的代码,但是由于某些原因,滚动条并没有真正出现?
final JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setBounds(10, 152, 456, 255);
textArea.setBorder(border);
textArea.setLineWrap(true);
sbrText = new JScrollPane(textArea);
sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
panel_1.add(textArea);发布于 2012-03-09 04:04:11
setBounds(),您必须删除使JTextArea在屏幕上具有绝对大小的代码行。这使得它不可调整大小,而JScrollPane只有在其内容可调整大小时才能工作。//错误的textArea.setBounds(10,152,456,255);
发布于 2012-03-09 04:09:02
看看这个
import javax.swing.*;
public class TestFrame extends JFrame
{
JTextAreaWithScroll textArea;
public TestFrame ()
{
super ("Test Frame");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setSize (300, 300);
textArea = new JTextAreaWithScroll (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add (textArea.getScrollPane ());
}
public static void main (String[] args)
{
SwingUtilities.invokeLater (new Runnable()
{
public void run ()
{
TestFrame f = new TestFrame ();
f.setVisible (true);
}
});
}
}
class JTextAreaWithScroll extends JTextArea
{
private JScrollPane scrollPane;
public JTextAreaWithScroll (int vsbPolicy, int hsbPolicy)
{
scrollPane = new JScrollPane (this, vsbPolicy, hsbPolicy);
}
public JScrollPane getScrollPane ()
{
return scrollPane;
}
}来自http://forum.html.it/forum/showthread/t-1035892.html
发布于 2012-03-09 04:07:45
您向父级添加了两次面板(scrollPane和TextArea )。将最后一行更改为
panel_1.add(sbrText);https://stackoverflow.com/questions/9624014
复制相似问题