简体   繁体   English

Java JScrollPane

[英]Java JScrollPane

I'm trying to add a Vertical scrolling my java programs textarea.我正在尝试添加一个垂直滚动我的 java 程序文本区域。 I am using this code to create my JScrollPane:我正在使用此代码来创建我的 JScrollPane:

console = my textarea.控制台 = 我的文本区域。

I am also Declaring JScrollPane vertical;我也声明 JScrollPane 垂直;

        vertical = new JScrollPane(console);
    vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    vertical.setVisible(true);
    this.add(vertical);

EDIT:编辑:

View of program:程序视图:

在此处输入图片说明 I'm new to Java but shouldn't that work and add a Vertical scroll bar to my textarea我是 Java 新手,但不应该这样工作,并向我的 textarea 添加一个垂直滚动条

What am I doing wrong?我究竟做错了什么?

Thanks for any help.谢谢你的帮助。

Here is an example:下面是一个例子:

在此处输入图片说明

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

public class ScrolledPane extends JPanel
{
    private JScrollPane vertical;
    private JTextArea console;

    public ScrolledPane()
    {
        setPreferredSize(new Dimension(200, 250));
        console = new JTextArea(15, 15);

        vertical = new JScrollPane(console);
        vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(vertical);
    }


    public static void main( String args[] )
    {
        new JFrame()
        {{
            getContentPane().add(new ScrolledPane());
            pack();
            setVisible(true);
        }};
    }
}

I think that in official tutorial about JTextArea and JScrollPane is described everything about that, another examples here and here我认为在关于JTextAreaJScrollPane 的官方教程中描述了关于它的一切, 这里这里的另一个例子

mySchroll = new JScrollPane(myTextArea, 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
import javax.swing.*;
import java.awt.*;

public class Demo extends JFrame {

    private JTextArea textArea;
    private JScrollPane scroll;

    Demo(){
        super("Write here...");
        textArea = new JTextArea();
        scroll = new JScrollPane(textArea);
        scroll.setVerticalScrollBarPolicy(22); /*22 is a const value for always vertical */
        add(scroll, BorderLayout.CENTER);

        setSize(270,270);
        setVisible(true);

    }

    public static void main(String[] args) {
        new Demo();
    }



}

Output of the code : See代码输出:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM