Java 删除 textarea 的水平滚动条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2987954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:17:52  来源:igfitidea点击:

removing horizontal scrollbar for textarea

javaswing

提问by charan

I'm using Java Swing. I have a textarea in a panel. I don't need a horizontal scrollbar for that textArea, only a vertical scrollbar is needed. I disabled auto scrollbar options, but still the horizontal scrollbar is working. Please help me in thz.

我正在使用 Java Swing。我在面板中有一个 textarea。我不需要那个 textArea 的水平滚动条,只需要一个垂直滚动条。我禁用了自动滚动条选项,但水平滚动条仍然有效。请帮助我。

采纳答案by Wajdy Essam

ta.setLineWrap(true)

Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped

设置文本区域的换行策略。如果设置为 true,如果线条太长而无法容纳在分配的宽度内,则这些线条将被换行。如果设置为 false,则行将始终展开

回答by dacwe

Two examples, the line wrap method and the scroll pane method:

两个例子,换行方法和滚动窗格方法:

public class Test {
    public static void main(String[] args) {

        // example text
        String rep = "The quick brown fox jumps over the lazy dog.";
        String all = rep;
        for(int i = 0; i < 100; i++) 
            all += "\n" + rep;

        // create the line wrap example
        JTextArea first = new JTextArea(all);
        first.setLineWrap(true);

        // create the scroll pane example
        JScrollPane second = 
            new JScrollPane(new JTextArea(all), 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

        // lay it out
        JFrame f = new JFrame("Test");
        f.setLayout(new GridLayout(1,2));
        f.add(first);
        f.add(second);
        f.setSize(400, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

回答by Marcus Becker

JTextPane don't have a method to enable or disable line wrap, one best choice is:

JTextPane 没有启用或禁用换行的方法,最好的选择是:

private JTextPane noWrapTextPane = new JTextPane() {
        @Override
        public boolean getScrollableTracksViewportWidth() {
            return getUI().getPreferredSize(this).width
                    <= getParent().getSize().width;
        }
    };

回答by Krish Nakum R

Scroll bar comes into text area when you are making your text area too small. This is because your the default columnnumber in netbeans text area is 20.

当您将文本区域设置得太小时,滚动条会进入文本区域。这是因为您在 netbeans 文本区域中的默认号是20

If you don't want the scroll bar being displayed , then take the textarea properties and change the column number to a value according to your size (say 10).

如果您不想显示滚动条,则使用 textarea 属性并将列号更改为根据您的大小(例如 10)的值。

And the scrollbar will not be shown.

并且不会显示滚动条。