我刚刚在GridBagLayout中为分割窗格做了一个布局。 工作得很好,看起来很正确。
然后,我需要垂直添加滚动条。因此我现在就这样做了。 然而,布局不像以前那样'大小'。 它现在延伸到相当于所显示的窗格区域。JSplitPane VPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,(new class1()),new JScrollPane(new class2(),ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
我需要让它看起来像以前一样。有什么想法吗?
答案 0 :(得分:4)
你所做的就是“隐藏”水平滚动条。这对管理组件的视图端口没有影响。
尝试在Scrollable
界面中包装现有布局。如果你不想实现自己的一个,你可以使用包装器容器......
public class ScrollableWrapper extends JPanel implements Scrollable {
private Component wrapper;
public ScrollableWrapper(Component wrapper) {
setLayout(new BorderLayout());
add(wrapper);
this.wrapper = wrapper;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return wrapper.getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 64;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 64;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
然后,我们将它添加到滚动窗格...
JSplitPane VPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,new class1(),new JScrollPane(new ScrollableWrapper(class2())));