如何通过自动滚动将 JTextPane 的插入位置居中?

发布于 2024-11-08 13:03:01 字数 113 浏览 2 评论 0原文

我想在 JTextPane 中自动滚动,以便带有插入符号(突出显示)的行居中。 我使用 Utilities.getRow(...caretPosition) 方法突出显示该行。 似乎是一个相当未知的问题。 谢谢!

I would like to auto scroll in my JTextPane so that the line with the caret (which is highlighted) is centered.
I am highlighting the line by using the Utilities.getRow(...caretPosition) method.
Seems to be a pretty unknown problem.
Thanks!

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

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

发布评论

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

评论(2

开始看清了 2024-11-15 13:03:01

看一下滚动窗格中的中心线。它将插入符号垂直居中,但将插入符号水平居中也很简单。

这是修改后的代码,因此它也水平居中:

public static void centerLineInScrollPane(JTextComponent component)
{
    Container container = SwingUtilities.getAncestorOfClass(JViewport.class, component);

    if (container == null) return;

    try
    {
        Rectangle r = component.modelToView(component.getCaretPosition());
        JViewport viewport = (JViewport)container;

        int extentWidth = viewport.getExtentSize().width;
        int viewWidth = viewport.getViewSize().width;

        int x = Math.max(0, r.x - (extentWidth / 2));
        x = Math.min(x, viewWidth - extentWidth);

        int extentHeight = viewport.getExtentSize().height;
        int viewHeight = viewport.getViewSize().height;

        int y = Math.max(0, r.y - (extentHeight / 2));
        y = Math.min(y, viewHeight - extentHeight);

        viewport.setViewPosition(new Point(x, y));
    }
    catch(BadLocationException ble) {}
}

Take a look at Center Line in Scroll Pane. It centers the caret vertically, but it is trivial to also center the caret horizontally.

Here is the code modified so it also centers horizontally:

public static void centerLineInScrollPane(JTextComponent component)
{
    Container container = SwingUtilities.getAncestorOfClass(JViewport.class, component);

    if (container == null) return;

    try
    {
        Rectangle r = component.modelToView(component.getCaretPosition());
        JViewport viewport = (JViewport)container;

        int extentWidth = viewport.getExtentSize().width;
        int viewWidth = viewport.getViewSize().width;

        int x = Math.max(0, r.x - (extentWidth / 2));
        x = Math.min(x, viewWidth - extentWidth);

        int extentHeight = viewport.getExtentSize().height;
        int viewHeight = viewport.getViewSize().height;

        int y = Math.max(0, r.y - (extentHeight / 2));
        y = Math.min(y, viewHeight - extentHeight);

        viewport.setViewPosition(new Point(x, y));
    }
    catch(BadLocationException ble) {}
}
百变从容 2024-11-15 13:03:01

我是这样调整的:

int y = Math.max(0, r.y - (extentHeight / 2));
            y = Math.min(y, viewHeight - extentHeight);
            int oldY = viewport.getViewPosition().y;
            for(int i = oldY;i<y;i++){
            viewport.setViewPosition(new Point(0, i));
                Thread.sleep(10);
            }

This is how i adjusted it:

int y = Math.max(0, r.y - (extentHeight / 2));
            y = Math.min(y, viewHeight - extentHeight);
            int oldY = viewport.getViewPosition().y;
            for(int i = oldY;i<y;i++){
            viewport.setViewPosition(new Point(0, i));
                Thread.sleep(10);
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文