【发布时间】:2011-04-04 07:05:07
【问题描述】:
我有一个实现 Scrollable 并使用 BorderLayout 的主 JPanel。它包含一个 NORTH 只读 JEditorPane、一个带有 FlowLayout 的 CENTER JPanel,其中 JButton 是动态添加的,还有一个 SOUTH JLabel,所有这些都按此顺序添加。当 CENTER JPanel 中添加了许多 JButton 时,按钮会自动换行:问题是 CENTER JPanel 占用的超过一行的垂直空间刚好与 SOUTH JLabel 重叠,不会导致整个 JPanel 动态增长或显示垂直滚动条。另一方面,当向 NORTH JEditorPane 添加足够多的文本以使其换行到下一行时,它会按我预期的方式运行,并将 CENTER JPanel 向下推并显示垂直滚动条。
这里主要的 JPanel Scrollable 实现:
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() {
if (getParent() instanceof JViewport) {
JViewport viewport = (JViewport) getParent();
return getPreferredSize().height < viewport.getHeight();
}
return false;
}
public boolean getScrollableTracksViewportWidth() {
return true;
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return Math.max(visibleRect.height / 10, 1);
}
我做错了什么?如何让 CENTER JPanel 将 SOUTH JLabel 向下推以增大整个主 JPanel?
【问题讨论】: