JScrollPane中JTextArea上的滚动条不起作用 [英] Scrollbars on JTextArea in a JScrollPane do not work

查看:144
本文介绍了JScrollPane中JTextArea上的滚动条不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让JTextArea滚动。我不知道你怎么会弄乱JScrollPane,但我似乎有,我只是看不到它。这是一个更大的项目的一部分,但下面的代码是我如何创建一个JTextArea并将其添加到JScrollPane。当您在文本区域的边缘外键入时,不会显示滚动条。将垂直滚动条设置为始终打开会产生一个不执行任何操作的滚动条。

I'm having trouble getting a JTextArea to scroll. I'm not sure how you can mess up a JScrollPane but I seem to have and I just can't see it. It's all part of a larger project but the code below is how I'm creating a JTextArea and adding it to a JScrollPane. When you type beyond the edge of the text area the scrollbar doesn't appear. Setting the vertical scrollbar to always on gives a scrollbar that doesn't do anything.

import javax.swing.*;
import java.awt.*;

public class TextAreaTest extends JFrame{

    public TextAreaTest() {
     super("Text Area Scroller");

     Container c = getContentPane();

     JTextArea textarea = new JTextArea();
     textarea.setPreferredSize(new Dimension(300, 50));
     textarea.setLineWrap(true);
     textarea.setText("xx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\nxx\n");

     JScrollPane scroller = new JScrollPane(textarea);

     c.add(scroller, BorderLayout.CENTER);
     pack();
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]){
     TextAreaTest instance = new TextAreaTest();
     instance.setVisible(true);
    }
}

我已尝试设置文本区域文本,或者构造函数中的行和列,两者都不起作用。它正在努力。任何想法?

I've tried setting the text area text, or rows and columns, in the constructor, neither of which worked. It's doing my head in. Any ideas?

推荐答案

设置滚动窗格的首选大小而不是文本区域。

Set the preferred size of the scroll pane rather than the text area.

这篇关于JScrollPane中JTextArea上的滚动条不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆