gpt4 book ai didi

java - 段落 View 首选项更改

转载 作者:行者123 更新时间:2023-12-02 08:06:31 31 4
gpt4 key购买 nike

我的 Swing 应用程序中有一个 JTextPane,其上方有一个 JSlider。当我拖动 slider 时,我希望当前具有插入符号的 JTextPane 段落减少/增加其宽度(并相应地调整高度)。我已经实现了以下代码,但似乎没有任何效果。我之前已经实现了一些与此类似的功能,但我想我现在缺少一些东西。谁能指出错误。

public class ExtendedParagraphView extends ParagraphView {
/** Maximum width of a single char (assuming monospaced font) {#{@value}}*/
public static final float CHARACTER_WIDTH = (float) 8.0;
/* DELTA is used to adjust the width of the paragraph view */
private float DELTA = (float) 0.0;
public ExtendedParagraphView(Element arg0) {
super(arg0);
}

/**
* Shift the paragraph to the left/right by an amount given by the
* difference between the end and the start position
* @param start
* @param end
*/
public void shiftParagraphView (int start, int end) {
/* The difference in pixel position is always calculated in terms of the
* difference between the end and start position */
DELTA = (float)(end - start) * ExtendedParagraphView.CHARACTER_WIDTH;
}

@Override
public float getPreferredSpan (int axis) {
if (axis == View.Y_AXIS) {
return super.getPreferredSpan(axis);
}
return super.getPreferredSpan(axis) - this.DELTA;
}
}

以下是调用 ExtendedParagraphView 内的 shiftParagraphView 方法的代码。

protected void shiftTextPaneElement (int start, int end) {
if (start == end) {
return;
}

int dot = this.textpane.getCaret().getDot();
int mark = this.textpane.getCaret().getMark();
int pos = mark <= dot ? mark : dot;

View view = textpane.getUI().getRootView(textpane);
int n = view.getViewCount();
for (int i = 0; i < n; i++) {
View v = view.getView(i);
if (v instanceof BoxView) {
int m = v.getViewCount();
for (int k = 0; k < m; k++) {
View vv = v.getView(k);
if (vv instanceof ExtendedParagraphView) {
((ExtendedParagraphView) vv).
shiftParagraphView(start, end);
return; //only a single paragraph is currently shifted
}
}
}
}
}

注意:经过一番尝试和错误后,以下内容似乎有效,

public void shiftParagraphView (int start, int end) {
DELTA = (float)(end - start) * ExtendedParagraphView.CHARACTER_WIDTH;
this.setInsets(getTopInset(), (short) (getLeftInset() + DELTA),
getBottomInset(), getRightInset());
this.getParent().preferenceChanged(this, true, false);
this.getContainer().repaint();
}

最佳答案

使用 yourStyledDocument.setParagraphAttributes() 而不是指定所需的左右缩进。

关于java - 段落 View 首选项更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8090945/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com