Log*_*gan 2 java user-interface swing
我正在尝试使用Java Swing创建一个文本编辑器.在那我使用JEditorPane而不是JTextArea.我在删除所选文本和从JEditorPane中替换所选文本时遇到问题.我正在使用的代码是:
public void delete(JEditorPane txt)
{
int start = txt.getSelectionStart();
int end = txt.getSelectionEnd();
String startText = txt.getText().substring(0,start);
String endText = txt.getText().substring(end,txt.getText().length());
txt.setText(startText + endText);
}
Run Code Online (Sandbox Code Playgroud)
我在这里面临的问题是,当我考虑来自getSelectionStart()和getSelectionEnd()的值时,他们不考虑换行符,但在使用子串时,正在考虑换行符.因此,如果我在一行之前使用此代码,在该行之前有5个换行符,则不会删除所选文本,而是从比所选文本少5的位置删除文本.替换也发生了同样的情况.请帮忙.
Jer*_*ome 11
使用JEditorPane.getDocument().remove()和JEditorPane.getDocument().insertString()
| 归档时间: |
|
| 查看次数: |
7257 次 |
| 最近记录: |