java panel滚动条设置_java – 当滚动条出现时,JScrollPane调整包含JPanel的大小

本文介绍了如何在Java中当滚动条出现时,正确调整JScrollPane中包含的JPanel大小。提供了一个名为ButtonContainerHost的自定义JPanel实现Scrollable接口的解决方案,该方案能确保滚动条的宽度被适当地加入到首选宽度,并且在空间允许的情况下允许JPanel展开。
摘要由CSDN通过智能技术生成

这是一个简单而不漂亮的解决方案:

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

编辑:

我认为这可能不适合你的情况.这是一个更好的解决方案,虽然它有很多样板:

private class ButtonContainerHost extends JPanel implements Scrollable {

private static final long serialVersionUID = 1L;

private final JPanel buttonContainer;

public ButtonContainerHost(JPanel buttonContainer) {

super(new BorderLayout());

this.buttonContainer = buttonContainer;

add(buttonContainer);

}

@Override

public Dimension getPreferredScrollableViewportSize() {

Dimension preferredSize = buttonContainer.getPreferredSize();

if (getParent() instanceof JViewport) {

preferredSize.width += ((JScrollPane) getParent().getParent()).getVerticalScrollBar()

.getPreferredSize().width;

}

return preferredSize;

}

@Override

public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {

return orientation == SwingConstants.HORIZONTAL ? Math.max(visibleRect.width * 9 / 10, 1)

: Math.max(visibleRect.height * 9 / 10, 1);

}

@Override

public boolean getScrollableTracksViewportHeight() {

if (getParent() instanceof JViewport) {

JViewport viewport = (JViewport) getParent();

return getPreferredSize().height < viewport.getHeight();

}

return false;

}

@Override

public boolean getScrollableTracksViewportWidth() {

return true;

}

@Override

public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {

return orientation == SwingConstants.HORIZONTAL ? Math.max(visibleRect.width / 10, 1)

: Math.max(visibleRect.height / 10, 1);

}

}

它实现了Scrollable以完全控制滚动,通过跟踪视口高度来确保按钮在空间可用时展开,并在任何时候将垂直滚动条的宽度添加到首选宽度.它可以在垂直滚动条可见时展开,但无论如何看起来都很糟糕.像这样用它:

scrollPane = new JScrollPane(new ButtonContainerHost(buttonContainer));

在我看来,由于javax.swing.ScrollPaneLayout中可能存在错误,因此需要使用此解决方法:

if (canScroll && (viewSize.height > extentSize.height)) {

prefWidth += vsb.getPreferredSize().width;

}

这里,extentSize设置为视口的首选大小,viewSize设置为viewport.getViewSize().这似乎不正确,AFAIK视口内视图的大小应始终等于首选大小.在我看来,视图大小应该与视口的实际大小而不是其首选大小进行比较.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值