JScrollpane 需要缩小宽度

发布于 2024-08-30 12:08:15 字数 213 浏览 5 评论 0原文

我有一个 JScrollpane,里面有一个 JPanel(面板包含一些 JLabel)。

我希望调整滚动窗格的大小以实际更改其大小(可能低于内部组件的首选大小),而不仅仅是视口的大小。

目标是当用户将滚动窗格缩小得太小时,内部面板会优雅地消失(在我的 miglayout 中使用特定的缩小优先级等)。

I have a JScrollpane that has a JPanel on the inside (and the panel contains some JLabels).

I want resizing the scroll pane to actually change its size (possibly below the preferred size of the inner components), not just the size of the viewport.

The goal is for the inner panel to gracefully disappear (using specific shrink priorities and the like in my miglayout) when the user shrinks the scrollpane too small.

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

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

发布评论

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

评论(1

离不开的别离 2024-09-06 12:08:15

也许最好的方法是让包含的组件始终与视口具有相同的宽度。为此,第一个包含的组件(作为 JViewPort 的子组件,传递到 JScrollPane 构造函数或设置为 viewportView)需要实现javax.swing.Scrollable。关键方法是 getScrollableTracksViewportWidth,它应该返回 true

这是一个快速但肮脏的可滚动 JPanel :

public class ScrollablePanel extends JPanel implements Scrollable {
    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
       return 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return ((orientation == SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width) - 10;
    }

    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
}

Probably the best method is to have the contained component always be the same width as the viewport. To do this the first contained component (the one that is the child to JViewPort, passed into the JScrollPane constructor or set as the viewportView) needs to implement javax.swing.Scrollable. The key method is getScrollableTracksViewportWidth, which should return true.

Here's a quick and dirty scrollable JPanel :

public class ScrollablePanel extends JPanel implements Scrollable {
    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
       return 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return ((orientation == SwingConstants.VERTICAL) ? visibleRect.height : visibleRect.width) - 10;
    }

    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

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