JEdi​​torPane 的自动缩放

发布于 2024-07-08 05:00:21 字数 269 浏览 9 评论 0原文

我使用 JEditorPane 作为编辑器在我的应用程序中编写注释。 内容类型设置为“文本/纯文本”。 当我在其中写入文本并且文本填充可用空间并且我继续键入时,文本不会向上移动以显示光标。 所以我不知道我在哪里输入以及我在输入什么,因为它是可见的。

您能告诉我如何通过向上移动上面的文本来始终显示插入符号吗?

相反,如果我可以在打字时自动调整编辑器的大小,那就更好了。 JEdi​​torPane 位于 JPanel 内部,因此我也必须调整其大小。 有任何想法吗?

I am using a JEditorPane as an editor to write comments in my application. The content type is set "text/plain". When I am writing text in it and the text fills the available space and I go on typing, the text is not moving upward to show the cursor. So I dont know where I am typing and what I am typing since it would be visible.

Could you tell me how to always show the caret by moving the above text upwards?

Instead, it could be better if I can auto-resize the editor as I am typing. The JEditorPane is inside a JPanel, so I have to resize that too. any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱要勇敢去追 2024-07-15 05:00:21

您需要将编辑器放入 JScrollPane 中。 ScrollPane 将自动添加滚动条,并且无需调整编辑器大小。

You need to put the editor inside a JScrollPane. The ScrollPane will automatically add scrollbars and remove the need to resize the editor.

浮生面具三千个 2024-07-15 05:00:21

编辑以添加完整的解决方案

您必须首先添加 JScrollPane。 然后,如果您不希望滚动条可见,但希望文本区域自动滚动,请

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

在滚动窗格上进行设置。 这将隐藏滚动条,但为您提供自动滚动。

以下是如何使用滚动窗格实现自动滚动,并自动调整大小到给定的最大值。

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;


public class SPTest extends JFrame {

    private static final long serialVersionUID = 1L;

    private JEditorPane editor;
    private JScrollPane scrollPane;
    private JPanel topPanel;
    private JLabel labelTop;

    public SPTest() {
        super("Editor test");
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void initComponents() {
        editor = new JEditorPane("text/plain", null);
        scrollPane = new JScrollPane(editor);
        topPanel = new JPanel();
        labelTop = new JLabel("main contents here");
        topPanel.add(labelTop);

        setSize(600, 400);
        editor.setMinimumSize(new Dimension(100, 30));
        editor.setPreferredSize(new Dimension(100, 60));
        scrollPane.setPreferredSize(new Dimension(600, 60));
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setMinimumSize(new Dimension(100, 30));

        final int MAX_HEIGHT_RSZ = 120;
        editor.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                int height = Math.min(editor.getPreferredSize().height, MAX_HEIGHT_RSZ);
                Dimension preferredSize = scrollPane.getPreferredSize();
                preferredSize.height = height;
                scrollPane.setPreferredSize(preferredSize);
                SPTest.this.validate();
            }
        });

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.SOUTH);
    }

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

}

您可以调整大小 您可以使用此 JScrollPane 而不是 JPanel 作为编辑器的容器。

Edited to add full solution

You have to add a JScrollPane first. Then, if you don't want the scrollbars to be visible, but you want the text area to autoscroll, set

scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);

on the scrollpane. This will hide the scrollbars, but provide you with autoscrolling.

Here's how you'd implement auto scrolling with scroll pane, and automatic resize upto a given maximum.

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;


public class SPTest extends JFrame {

    private static final long serialVersionUID = 1L;

    private JEditorPane editor;
    private JScrollPane scrollPane;
    private JPanel topPanel;
    private JLabel labelTop;

    public SPTest() {
        super("Editor test");
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    private void initComponents() {
        editor = new JEditorPane("text/plain", null);
        scrollPane = new JScrollPane(editor);
        topPanel = new JPanel();
        labelTop = new JLabel("main contents here");
        topPanel.add(labelTop);

        setSize(600, 400);
        editor.setMinimumSize(new Dimension(100, 30));
        editor.setPreferredSize(new Dimension(100, 60));
        scrollPane.setPreferredSize(new Dimension(600, 60));
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setMinimumSize(new Dimension(100, 30));

        final int MAX_HEIGHT_RSZ = 120;
        editor.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                int height = Math.min(editor.getPreferredSize().height, MAX_HEIGHT_RSZ);
                Dimension preferredSize = scrollPane.getPreferredSize();
                preferredSize.height = height;
                scrollPane.setPreferredSize(preferredSize);
                SPTest.this.validate();
            }
        });

        setLayout(new BorderLayout());
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.SOUTH);
    }

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

}

You could resize You can use this JScrollPane instead of the JPanel as the container for the editor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文