简体   繁体   English

如何使用Eclipse在Java GUI JTextArea中滚动?

[英]How to have scrolling in Java GUI JTextArea with Eclipse?

I am trying to put an muti lines text in the JtextArea , but there isn't a scrolling so I cannot move to see any data below the default JtextArea area, here is the code and textArea_1 is the JtextArea : 我试图在JtextArea放置多行文本,但是没有滚动,所以我无法移动以查看默认JtextArea区域下面的任何数据,这里是代码, textArea_1JtextArea

String abc="";
for(int i=0; i<=100; i++){
abc = abc + data[i][0]+"\n";
}
textArea_1.setText(abc);

You need to look into adding a JScrollPane . 您需要考虑添加JScrollPane

Link: http://docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html 链接: http//docs.oracle.com/javase/7/docs/api/javax/swing/JScrollPane.html

import javax.swing.JScrollPane;
JScrollPane scrollPane = new JScrollPane(TEXTAREAHERE);

Scrolling is handled by JScrollPane . 滚动由JScrollPane处理。

Check out How to use scroll panes and for your own reference, JScrollPane and which is also demonstrated in How to use Text Areas 查看如何使用滚动窗格并供您自己参考, JScrollPane以及如何使用文本区域中的演示

Updated with example 更新了示例

在此输入图像描述

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPane04 {

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

    public TestScrollPane04() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JTextArea textArea = new JTextArea(10, 20);
                String abc = "";
                for (int i = 0; i <= 100; i++) {
                    abc = abc + "This is some additional text " + i + "\n";
                }
                textArea.setText(abc);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(textArea));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

You should add a JScrollPane . 您应该添加一个JScrollPane as

JscrollPane myScroll=new JScrollPane(textArea_1);

If your text is more than that can see other text by scrolling. 如果您的文本不止于此,则可以通过滚动查看其他文本。 By default when you set or append text to JTextArea it scrolls to end (you can see that if text is large so that scroll bar appeard). 默认情况下,当您向JTextArea setappend文本时,它会滚动到结尾(您可以看到,如果文本很大,则滚动条会显示)。 So to scroll back to first line try 所以要回滚到第一行试试

textArea_1.setCaretPosition(0);

see Java doc 请参阅Java doc

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

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