【发布时间】:2010-03-30 11:35:20
【问题描述】:
我已经创建了 JTextpane 并在 textpane 中插入了组件(像 Jtextarea 这样的组件)。当我在该 JTextpane 中插入新组件时,(JTextpane 的)Jscrollpane 的(垂直滚动条)会自动设置为底部。我想让它保持在顶部位置。我该怎么做呢
谢谢 苏尼尔·库马尔·萨胡
【问题讨论】:
我已经创建了 JTextpane 并在 textpane 中插入了组件(像 Jtextarea 这样的组件)。当我在该 JTextpane 中插入新组件时,(JTextpane 的)Jscrollpane 的(垂直滚动条)会自动设置为底部。我想让它保持在顶部位置。我该怎么做呢
谢谢 苏尼尔·库马尔·萨胡
【问题讨论】:
这是我使用的一个实用程序类。它可用于滚动到JScrollPane 的顶部、底部、左侧、右侧或水平/垂直中心。
public final class ScrollUtil {
public static final int NONE = 0, TOP = 1, VCENTER = 2, BOTTOM = 4, LEFT = 8, HCENTER = 16, RIGHT = 32;
private static final int OFFSET = 100; // Required for hack (see below).
private ScrollUtil() {
}
/**
* Scroll to specified location. e.g. <tt>scroll(component, BOTTOM);</tt>.
*
* @param c JComponent to scroll.
* @param part Location to scroll to. Should be a bit-wise OR of one or moe of the values:
* NONE, TOP, VCENTER, BOTTOM, LEFT, HCENTER, RIGHT.
*/
public static void scroll(JComponent c, int part) {
scroll(c, part & (LEFT|HCENTER|RIGHT), part & (TOP|VCENTER|BOTTOM));
}
/**
* Scroll to specified location. e.g. <tt>scroll(component, LEFT, BOTTOM);</tt>.
*
* @param c JComponent to scroll.
* @param horizontal Horizontal location. Should take the value: LEFT, HCENTER or RIGHT.
* @param vertical Vertical location. Should take the value: TOP, VCENTER or BOTTOM.
*/
public static void scroll(JComponent c, int horizontal, int vertical) {
Rectangle visible = c.getVisibleRect();
Rectangle bounds = c.getBounds();
switch (vertical) {
case TOP: visible.y = 0; break;
case VCENTER: visible.y = (bounds.height - visible.height) / 2; break;
case BOTTOM: visible.y = bounds.height - visible.height + OFFSET; break;
}
switch (horizontal) {
case LEFT: visible.x = 0; break;
case HCENTER: visible.x = (bounds.width - visible.width) / 2; break;
case RIGHT: visible.x = bounds.width - visible.width + OFFSET; break;
}
// When scrolling to bottom or right of viewport, add an OFFSET value.
// This is because without this certain components (e.g. JTable) would
// not scroll right to the bottom (presumably the bounds calculation
// doesn't take the table header into account. It doesn't matter if
// OFFSET is a huge value (e.g. 10000) - the scrollRectToVisible method
// still works correctly.
c.scrollRectToVisible(visible);
}
}
【讨论】:
我发现最简单的方法如下:
public void scroll(int vertical) {
switch (vertical) {
case SwingConstants.TOP:
getVerticalScrollBar().setValue(0);
break;
case SwingConstants.CENTER:
getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
getVerticalScrollBar().setValue(getVerticalScrollBar().getValue() / 2);
break;
case SwingConstants.BOTTOM:
getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
break;
}
}
我将它放在扩展 JScrollPane 的对象中,但您也可以在所有 getVertivalScrollBar() 之前添加 JScrollPane 的名称。 CENTER 有两个setValue()s,因为getMaximum() 返回JScrollBar 的底部,而不是它的最小值。这也适用于使用getHorizontalScrollBar() 代替getverticalScrollBar() 的水平滚动。
【讨论】:
应该可以将DefaultCaret 更新策略设置为NEVER_UPDATE。其他用途见文章Text Area Scrolling。
【讨论】:
您可以使用多种方法,具体取决于滚动窗格中的内容。见the tutorial,最后一节。
【讨论】:
这也有效:
JTextArea. myTextArea;
// ...
myTextArea.select(0, 0); // force the scroll value to the top
【讨论】:
jScrollPane.getVerticalScrollBar().setValue(1);
【讨论】:
ScrollPane 的滚动值始终介于 (0.0 - 1)
例如
0.0 = 0%
0.1 = 10%
0.2 = 20%
0.25 = 25%
....等等
您可以使用这些值调整滚动位置。例如,在 JavaFX 中
// suppose this is the scrollpane
ScrollPane pane = new ScrollPane();
// to scroll the scrollpane horizontally 10% from its current position
pane.setHvalue(pane.getHvalue() + 0.1);
// to scroll 100%
pane.setHvalue(1);
and so on...
根据需要应用逻辑
【讨论】: