本文整理汇总了Java中javax.swing.text.Caret.setMagicCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java Caret.setMagicCaretPosition方法的具体用法?Java Caret.setMagicCaretPosition怎么用?Java Caret.setMagicCaretPosition使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Caret
的用法示例。
在下文中一共展示了Caret.setMagicCaretPosition方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: resetCaretMagicPosition
import javax.swing.text.Caret; //导入方法依赖的package包/类
public static void resetCaretMagicPosition(JTextComponent component) {
Caret caret;
if (component != null && (caret = component.getCaret()) != null) {
caret.setMagicCaretPosition(null);
}
}
示例2: actionPerformedImpl
import javax.swing.text.Caret; //导入方法依赖的package包/类
@Override
public void actionPerformedImpl(ActionEvent e, RTextArea textArea) {
Rectangle visible = textArea.getVisibleRect();
Rectangle newVis = new Rectangle(visible);
int selectedIndex = textArea.getCaretPosition();
int scrollAmount = textArea.getScrollableBlockIncrement(
visible, SwingConstants.VERTICAL, direction);
int initialY = visible.y;
Caret caret = textArea.getCaret();
Point magicPosition = caret.getMagicCaretPosition();
int yOffset;
if (selectedIndex!=-1) {
try {
Rectangle dotBounds = textArea.modelToView(selectedIndex);
int x = (magicPosition != null) ? magicPosition.x :
dotBounds.x;
int h = dotBounds.height;
yOffset = direction *
((int)Math.ceil(scrollAmount/(double)h)-1)*h;
newVis.y = constrainY(textArea, initialY+yOffset, yOffset, visible.height);
int newIndex;
if (visible.contains(dotBounds.x, dotBounds.y)) {
// Dot is currently visible, base the new
// location off the old, or
newIndex = textArea.viewToModel(
new Point(x, constrainY(textArea,
dotBounds.y + yOffset, 0, 0)));
}
else {
// Dot isn't visible, choose the top or the bottom
// for the new location.
if (direction == -1) {
newIndex = textArea.viewToModel(new Point(
x, newVis.y));
}
else {
newIndex = textArea.viewToModel(new Point(
x, newVis.y + visible.height));
}
}
newIndex = constrainOffset(textArea, newIndex);
if (newIndex != selectedIndex) {
// Make sure the new visible location contains
// the location of dot, otherwise Caret will
// cause an additional scroll.
adjustScrollIfNecessary(textArea, newVis, initialY,
newIndex);
if (select) {
textArea.moveCaretPosition(newIndex);
}
else {
textArea.setCaretPosition(newIndex);
}
}
} catch (BadLocationException ble) { }
} // End of if (selectedIndex!=-1).
else {
yOffset = direction * scrollAmount;
newVis.y = constrainY(textArea, initialY + yOffset, yOffset, visible.height);
}
if (magicPosition != null) {
caret.setMagicCaretPosition(magicPosition);
}
textArea.scrollRectToVisible(newVis);
}