我正在创建一个GUI,我在其中将水平滚动条添加到TextArea中,因为将在TextArea中显示的标签/行的长度大于TextArea的宽度。
这是我创建窗格的代码。但似乎没有任何事情发生......
//为描述创建文本区域并添加到主框架
public static JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setPreferredSize(new Dimension(100,600));
scrollpanel = new JScrollPane(textArea);
scrollpanel.setBounds(20, 600, 920, 130);
scrollpanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollpanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollpanel.setBorder(BorderFactory.createTitledBorder("Description"));
frmToolToMigrate.getContentPane().add(scrollpanel);
答案 0 :(得分:3)
而不是setBounds()
,覆盖滚动窗格的getPreferredSize()
,如图所示here。默认情况下,滚动条将根据需要自动显示。另请考虑实施Scrollable
接口。
答案 1 :(得分:2)
你看过
吗?http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
您还可以将textArea作为扩展TextArea java类的新类,然后实现可滚动。
public class MyTextArea extends JTextArea implements Scrollable{
//Whatever you want to do
}