我创建了一个textarea,我需要在必要时将一个滚动条应用到textarea(当文本太长而且无法再读取时)。
这是我编写的代码,但出于某种原因,滚动条真的没有出现?
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);
答案 0 :(得分:1)
由于使用JTextArea
,您必须删除使setBounds()
在屏幕上具有绝对大小的代码行。这使得不可调整大小,JScrollPane
仅在其内容可调整大小时才有效。
// wrong
textArea.setBounds(10, 152, 456, 255);
请阅读JTextArea和JScrollPane教程;请从两个教程中运行示例。
答案 1 :(得分:1)
看到这个
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;
}
}
答案 2 :(得分:0)
您将TextArea添加到父级两次(scrollPane和面板)。 将最后一行更改为
panel_1.add(sbrText);
答案 3 :(得分:0)
确保<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myctrl">
<p>{{ foo }}</p>
<div ng-show="true">
$scope.true is equal to false here. ng-show should hide this.
</div>
<div ng-show="true2">
$scope.true2 is equal to false here. ng-show hides this.
</div>
<div ng-hide="false">
$scope.false is equal to true here. ng-hide should hide this.
</div>
<div ng-hide="false2">
$scope.false2 is equal to true here. ng-hide hides this.
</div>
<div ng-hide="undefined">
$scope.undefined is equal to true here. ng-hide should hide this.
</div>
<div ng-hide="undefined2">
$scope.undefined2 is equal to true here. ng-hide hides this.
</div>
</div>
和preferredSize
相同。滚动窗格将使用viewportSize
的首选大小调整自身大小,如果文本区域的首选大小足以显示自身,则可能导致滚动条消失。
再次,请阅读JTextArea和JScrollPane教程。
textArea