如何在 Java Swing 中自动滚动到底部

发布于 2024-11-15 23:40:01 字数 250 浏览 3 评论 0原文

我有一个简单的 JPanel,上面有一个 JScrollPane(根据需要带有垂直滚动条)。

东西被添加到 JPanel(或从中删除),当它超出面板底部时,我希望 JScrollPane 根据需要自动向下滚动到底部,或者如果某些组件离开面板则向上滚动。

我该怎么做?我猜我需要某种监听器,每当 JPanel 高度发生变化时就会调用它?或者有像 JScrollPanel.setAutoScroll(true) 这样简单的东西吗?

I have a simple JPanel with a JScrollPane (with vertical scrollbar as needed) on it.

Things get added to (or removed from) the JPanel and when it goes beyond the bottom of the panel, I want the JScrollPane to scroll down to the bottom automatically as needed or scroll up if some components go away from the panel.

How shall I do this? I am guessing I need some kind of listener which gets called whenever the JPanel height changes? Or is there something as simple as JScrollPanel.setAutoScroll(true)?

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

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

发布评论

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

评论(3

寄居人 2024-11-22 23:40:01
scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
        public void adjustmentValueChanged(AdjustmentEvent e) {  
            e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
        }
    });

这将是最好的。从JScrollPane和JList自动滚动找到

scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {  
        public void adjustmentValueChanged(AdjustmentEvent e) {  
            e.getAdjustable().setValue(e.getAdjustable().getMaximum());  
        }
    });

This would be the best. Found from JScrollPane and JList auto scroll

雄赳赳气昂昂 2024-11-22 23:40:01

当您添加/删除面板的组件时,您应该在面板上调用 revalidate() 以确保组件布局正确。

然后,如果您想滚动到底部,那么您应该可以使用:

JScrollBar sb = scrollPane.getVerticalScrollBar();
sb.setValue( sb.getMaximum() );

When you add/remove components for a panel you should invoke revalidate() on the panel to make sure the components are laid out properly.

Then, if you want to scroll to the bottom then you should be able to use:

JScrollBar sb = scrollPane.getVerticalScrollBar();
sb.setValue( sb.getMaximum() );
咽泪装欢 2024-11-22 23:40:01

这就是我自动向上或向下滚动的方式:

/**
 * Scrolls a {@code scrollPane} all the way up or down.
 *
 * @param scrollPane the scrollPane that we want to scroll up or down
 * @param direction  we scroll up if this is {@link ScrollDirection#UP},
 *                   or down if it's {@link ScrollDirection#DOWN}
 */
public static void scroll(JScrollPane scrollPane, ScrollDirection direction) {
    JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
    // If we want to scroll to the top, set this value to the minimum,
    // else to the maximum
    int topOrBottom = direction == ScrollDirection.UP ?
                      verticalBar.getMinimum() :
                      verticalBar.getMaximum();

    AdjustmentListener scroller = new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            Adjustable adjustable = e.getAdjustable();
            adjustable.setValue(topOrBottom);
            // We have to remove the listener, otherwise the
            // user would be unable to scroll afterwards
            verticalBar.removeAdjustmentListener(this);
        }
    };
    verticalBar.addAdjustmentListener(scroller);
}

public enum ScrollDirection {
    UP, DOWN
}

This is how I scroll all the way up or down automatically:

/**
 * Scrolls a {@code scrollPane} all the way up or down.
 *
 * @param scrollPane the scrollPane that we want to scroll up or down
 * @param direction  we scroll up if this is {@link ScrollDirection#UP},
 *                   or down if it's {@link ScrollDirection#DOWN}
 */
public static void scroll(JScrollPane scrollPane, ScrollDirection direction) {
    JScrollBar verticalBar = scrollPane.getVerticalScrollBar();
    // If we want to scroll to the top, set this value to the minimum,
    // else to the maximum
    int topOrBottom = direction == ScrollDirection.UP ?
                      verticalBar.getMinimum() :
                      verticalBar.getMaximum();

    AdjustmentListener scroller = new AdjustmentListener() {
        @Override
        public void adjustmentValueChanged(AdjustmentEvent e) {
            Adjustable adjustable = e.getAdjustable();
            adjustable.setValue(topOrBottom);
            // We have to remove the listener, otherwise the
            // user would be unable to scroll afterwards
            verticalBar.removeAdjustmentListener(this);
        }
    };
    verticalBar.addAdjustmentListener(scroller);
}

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