简体   繁体   English

Java JTextArea允许滚动到文本末尾

[英]Java JTextArea allow scrolling beyond end of text

Java's text areas don't allow scrolling beyond the end of the text normally, but sometimes it is more comfortable to allow ie the bottom of the text to be at the middle of the window. Java的文本区域通常不允许滚动超出文本的末尾,但有时允许更舒适,即文本的底部位于窗口的中间。 Is there an easy way to do this? 是否有捷径可寻?

If I understand you correctly, you could simply set the row property of the JTextArea to be large enough so that the text does not completely fill it. 如果我理解正确,您可以简单地将JTextArea的row属性设置为足够大,以便文本不会完全填充它。

eg, 例如,

import javax.swing.*;

public class MyLargeTextArea extends JPanel {

    private static final int ROWS = 20;
    private static final int COLS = 40;
    private JTextArea textArea = new JTextArea(ROWS, COLS);

    public MyLargeTextArea() {
        textArea.setText("This is a small amount of text");

        // so lines wrap
        textArea.setWrapStyleWord(true);
        textArea.setLineWrap(true);

        add(new JScrollPane(textArea));
    }

    private static void createAndShowGui() {
        MyLargeTextArea mainPanel = new MyLargeTextArea();

        JFrame frame = new JFrame("MyLargeTextArea");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}

What you should never do is to set the size, bounds, or preferredSize of a JTextArea as that would inhibit its desired behavior to expand as needed if text is added and filling it with text beyond its current visualized size. 您应该永远做的是设置JTextArea的大小,边界或preferredSize,因为如果添加文本并使用超出当前可视化大小的文本填充文本,则会阻止其按需扩展所需的行为。

Edit: 编辑:

This is how eg Visual Studio behaves. 这就是Visual Studio的行为方式。 You can have the last line of the text as the first line of the scrolling area 您可以将文本的最后一行作为滚动区域的第一行

You can adjust the preferred height of the text area to allow the last line to be scrolled to the top of the text area: 您可以调整文本区域的首选高度,以允许最后一行滚动到文本区域的顶部:

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

public class TextAreaTop
{
    private static void createAndShowGUI()
    {
        JTextArea textArea = new JTextArea(6, 30)
        {
            @Override
            public Dimension getPreferredSize()
            {
                Dimension preferred = super.getPreferredSize();
                int heightAdjustment = getParent().getSize().height - getRowHeight();
                preferred.height += heightAdjustment;

                Border border = getBorder();

                if (border != null)
                    preferred.height -= border.getBorderInsets(this).bottom;

                return preferred;
            }
        };

        textArea.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n0");
        textArea.setBorder( new EmptyBorder(20, 20, 20, 20) );

        JScrollPane scrollPane = new JScrollPane( textArea );

        JFrame frame = new JFrame("TextAreaTop");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(scrollPane);
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}




Original answer (which can probably be ignored): 原始答案(可以忽略):

the bottom of the text to be at the middle of the window 文本的底部位于窗口的中间

If the bottom of the text is always at the middle that means the bottom half of the text area will never be used so you have wasted space. 如果文本的底部始终位于中间,则意味着文本区域的下半部分将永远不会被使用,因此您会浪费空间。 I would think this will confuse the user. 我认为这会让用户感到困惑。

However, you can add a Border to the text area. 但是,您可以向文本区域添加Border This will allow you to leave a little extra space at the bottom when fully scrolled: 这将允许您在完全滚动时在底部留出一些额外的空间:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.border.*;

public class SSCCE extends JPanel
{
    public SSCCE()
    {
        setLayout( new BorderLayout() );

        JTextArea textArea = new JTextArea(10, 30);
        textArea.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n0");
        textArea.setBorder( new EmptyBorder(0, 0, 48, 0) );

        add( new JScrollPane(textArea) );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SSCCE());
        frame.setLocationByPlatform( true );
        frame.setSize(400, 200);
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
    }
}

If you want to get a little fancy you could also add the following. 如果你想有点想象,你也可以添加以下内容。 This code will attempt to center the line where the caret is located. 此代码将尝试将插入符号所在的行居中。

textArea.addCaretListener( new CaretListener()
{
    public void caretUpdate(CaretEvent e)
    {
        JTextComponent component = (JTextComponent)e.getSource();
        RXTextUtilities.centerLineInScrollPane( component );
    }
});

To use the above code you will need the Text Utilities class. 要使用上面的代码,您需要Text Utilities类。

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

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