Java JScrollPane

倖福魔咒の 提交于 2020-01-03 09:09:25

问题


I'm trying to add a Vertical scrolling my java programs textarea. I am using this code to create my JScrollPane:

console = my textarea.

I am also Declaring JScrollPane vertical;

        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

What am I doing wrong?

Thanks for any help.


回答1:


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);
        }};
    }
}



回答2:


I think that in official tutorial about JTextArea and JScrollPane is described everything about that, another examples here and here

mySchroll = new JScrollPane(myTextArea, 
    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);



回答3:


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



来源:https://stackoverflow.com/questions/7766844/java-jscrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!