public static void setJTextPaneFont(JTextPane jtp, Color c, int start_index,int end_index) {
MutableAttributeSet attrs = jtp.getInputAttributes();
StyleConstants.setForeground(attrs, c);
StyledDocument doc = jtp.getStyledDocument();
doc.setCharacterAttributes(start_index, end_index, attrs, false);
}
上記のコードを作成して、開始インデックスと終了インデックスを入力したときに特定の単語の前景を変更しました。しかし、行番号、start_index、および終了インデックスを渡すときに前景を変更する必要があります。行番号を入力するときに特定の行を識別する方法。
public void gotoStartOfLine(JTextComponent component, int line) {
Element root = component.getDocument().getDefaultRootElement();
line = Math.max(line, 1);
line = Math.min(line, root.getElementCount());
component.setCaretPosition(root.getElement(line - 1).getStartOffset());
}
特定の行に移動するために上記のコードを試しましたが、うまくいきませんでした