问题描述
我刚刚在GridBagLayout中为拆分窗格进行了布局.工作正常,看上去不错.
I'd just made a layout in GridBagLayout for a split pane.Worked perfectly and looked right.
然后我需要仅垂直添加滚动条.因此,我现在已经做到了.但是,布局不会像以前那样调整大小".现在,它延伸到相当宽的位置,直到显示的窗格区域.
I then needed to then add a scroll bar vertically only. Hence I have done that now.However the layout doesn't 'size' like before.It now stretches across rather sticking to the area of the pane shown.
JSplitPane VPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,(new class1()),new JScrollPane(new class2(),ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
我需要使它看起来像以前一样.有什么想法吗?
I need to make it look like it did before. Any ideas?
推荐答案
您要做的只是隐藏"水平滚动条.这将不会对管理您的组件的查看端口产生影响.
All you've done is "hide" the horizontal scroll bar. This will have no effect on the view port that is managing your component.
尝试将现有布局包装在Scrollable
界面中.如果您不想自己实现自己,可以改用包装容器...
Try wrapping your existing layout in a Scrollable
interface. If you don't want to implement one your self, you could use a wrapper container instead...
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;
}
}
然后,我们将其添加到滚动窗格中...
Then, we you add it to your scroll pane...
JSplitPane VPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,new class1(),new JScrollPane(new ScrollableWrapper(class2())));
这篇关于ScrollPane Java Swing中的GridbagLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!