写在JTextArea的底部 [英] Writing at the bottom of JTextArea

查看:168
本文介绍了写在JTextArea的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JScrollPane 和一个 JTextArea (可编辑)有10行。我希望第一个用户输入显示在 JTextArea 的底部,最近的输入应该向上推送上一个输入。为了实现这一点,我使用 textArea.setMargin(new Insets(x,0,0,0)); 并且一切正常 - 除了我的第二个输入将切换 JScrollPane

I have a JScrollPane and a JTextArea (editable) with 10 rows. I want the first user input to appear at the bottom of the JTextArea and the most recent input should push the previous input upward. To achieve this I use textArea.setMargin(new Insets(x,0,0,0)); and everything works as it should - except that my second input will toggle the JScrollPane.

如何从 JTextArea的底部开始并且仅在整个原始视口填充时启用滚动?

How can I start at the bottom of the JTextArea and only enable scrolling when the entire original viewport is filled?

推荐答案

基本上,您可以添加 JTextText JPanel 上,另一个 JPanel 作为填充符,导致 JTextArea 占用它实际需要的最小空间。

Basically, you could add the JTextText onto a JPanel with another JPanel which acts a filler, causing the JTextArea to occupy the smallest space it actually needs.

我是通过使用 GridBagLayout 强迫填充占据大部分空间......

I did this by using GridBagLayout to force the fill to occupy the most of space it could...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTextArea {

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

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

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

    public class TestPane extends JPanel {

        private JTextArea ta;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;

            JPanel filler = new JPanel();
            filler.setBackground(Color.WHITE);
            add(filler, gbc);

            gbc.gridy = 1;
            gbc.weighty = 0;
            ta = new JTextArea(1, 20);
            add(ta, gbc);
        }
    }

}

这篇关于写在JTextArea的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆