JScrollpane需要缩小其宽度 [英] JScrollpane needs to shrink its width

查看:100
本文介绍了JScrollpane需要缩小其宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JScrollpane ,内部有一个 JPanel (该面板包含一些 JLabel s)。

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.

目标是当用户缩小时内部面板正常消失(使用我的miglayout中的特定缩小优先级等)滚动窗格太小。

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.

推荐答案

可能最好的方法是让包含的组件始终与视口的宽度相同。要做到这一点,第一个包含的组件(一个孩子到 JViewPort ,传递到 JScrollPane 构造函数或集合因为 viewportView )需要实现 javax.swing.Scrollable 。关键方法是 getScrollableTracksViewportWidth ,它应返回 true

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.

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

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;
    }
}

这篇关于JScrollpane需要缩小其宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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