如何在仅垂直滚动窗格中布局组件? [英] How to layout a components in vertical only scrollpane?

查看:22
本文介绍了如何在仅垂直滚动窗格中布局组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的问题,我在任何地方都找不到答案.

I have a complex question I cant find an answer anywhere.

这是一个例子:

public static void main(final String[] args) {
    final JFrame frame = new JFrame("test");
    final JPanel bigPane = new JPanel(new GridLayout(0, 1));
    frame.setContentPane(new JScrollPane(bigPane, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
    bigPane.add(new JTextField("reterterter ertertrert erterter ert"));
    bigPane.add(new JTextField(" ertertrert erterter ert"));
    bigPane.add(new JTextField("reterterter ertertrert  ert"));
    bigPane.add(new JTextField("reterterter ertertrert erterter "));
    frame.pack();
    frame.setSize(frame.getSize().width/2, frame.getSize().height);
    frame.setVisible(true);
}

在这里,显然文本字段被剪切了.更准确地说,bigPane 被剪掉了.但我想调整文本字段的大小而不是剪切,所以我们仍然可以使用它们.

Here, clearly the textfields are cut. More precisely, bigPane is cut. But I would like the textfields to be resized instead of cut, so we can still use them.

由于禁止出现水平滚动条,我希望布局忘记视图(bigPane)的首选宽度并始终强制视图的宽度与视口的宽度相匹配.就像水平方向没有虚拟视口一样,只有垂直方向.

Since the horizontal scrollbar is forbidden to appear, I would like the layout to forget the view's (bigPane) preferred width and always force the view's width to match the viewport's width. Like there would be no virtual viewport for the horizontal direction, only in the vertical.

我找不到解决方案,甚至找不到 hack,如果您能提供帮助,谢谢.

I can't find a solution to that, and not even a hack, thaks if you can help.

妮可.

PS:看起来 ViewportLayout 的代码不包含任何代码

PS: looks like the code for ViewportLayout doesn't contain any code for that

PS2 :这是上述代码的结果上限 http://www.nraynaud.com/kilombo/testLayout.png 见右侧.

PS2 : here is a cap of the result of the above code http://www.nraynaud.com/kilombo/testLayout.png see the right side.

推荐答案

我认为正确"的方法是使用 Scrollable 接口:

I think the "correct" way to do this is to use the Scrollable interface:

public class ScrollPaneWidthTrackingPanel extends JPanel implements Scrollable {
    private static final long serialVersionUID = 1L;

    public ScrollPaneWidthTrackingPanel(LayoutManager layoutManager) {
        super(layoutManager);
    }

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return Math.max(visibleRect.height * 9 / 10, 1);
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return Math.max(visibleRect.height / 10, 1);
    }
}

你的两行代码必须改为:

Two lines of your code then have to change to:

final ScrollPaneWidthTrackingPanel bigPane = new ScrollPaneWidthTrackingPanel(new GridLayout(0, 1));
frame.setContentPane(new JScrollPane(bigPane));

无论框架的大小如何,组件都应始终填充 JScrollPane 的整个宽度.此更改有一个副作用,即组件现在不会填充 JScrollPane 的高度.要解决该问题,请将以下代码添加到 getScrollableTracksViewportHeight 方法的顶部:

And the components should always fill the entire width of the JScrollPane, whatever the size of the frame. This change has a side effect that the components will now not fill the height of the JScrollPane. To solve that add the following code to the top of the getScrollableTracksViewportHeight method:

if (getParent() instanceof JViewport) {
    JViewport viewport = (JViewport) getParent();
    return component.getPreferredSize().height < viewport.getHeight();
}

这篇关于如何在仅垂直滚动窗格中布局组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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