簡體   English   中英

Java JScrollPane

[英]Java JScrollPane

我正在嘗試添加一個垂直滾動我的 java 程序文本區域。 我正在使用此代碼來創建我的 JScrollPane:

控制台 = 我的文本區域。

我也聲明 JScrollPane 垂直;

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

編輯:

程序視圖:

在此處輸入圖片說明 我是 Java 新手,但不應該這樣工作,並向我的 textarea 添加一個垂直滾動條

我究竟做錯了什么?

謝謝你的幫助。

下面是一個例子:

在此處輸入圖片說明

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

我認為在關於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();
    }



}

代碼輸出:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM