[英]restrict JScrollPane view width
如何限制JScrollPanel
視圖寬度? 在任何情況下我都不希望水平滾動發生。 我試圖提供自己的JViewport
但這並沒有多大幫助。
private static class WidthRestrictingViewport extends JViewport {
private Container contents;
public WidthRestrictingViewport(final Container cnts) {
contents = cnts;
}
@Override
public Dimension getPreferredSize() {
if (getParent() instanceof JScrollPane) {
JScrollPane sp = (JScrollPane) getParent();
return new Dimension(sp.getWidth(), contents.getHeight());
}
return super.getPreferredSize();
}
@Override
public Dimension getMaximumSize() {
return getPreferredSize();
}
}
以及創建JScrollPane
代碼
Container fpdp = ....
JScrollPane sp = new JScrollPane();
sp.setViewport(new WidthRestrictingViewport(fpdp));
sp.setViewportView(fpdp);
我提供的contents
實例占用了它的最大寬度,但我希望它的寬度不超過JScrollPane
寬度,因此水平滾動不會發生。 我究竟做錯了什么?
對不起,我的解釋很糟糕。 我找到了一種方法。 感謝JScrollpane需要縮小其寬度 。
我遇到的另一個問題是ScrollablePanel
錯誤布局管理器。 那就是FlowLayout
搞砸了所有事情。 我立刻不理解它,並認為問題是Scrollable
接口使用。
所以我只是把ScrollablePanel
和它的布局管理器設置為BorderLayout
,現在一切都很好!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.