如何使用JEditorPane
獲取選定文本的索引?使用JEditorPane獲取選定文本的索引
我想在編輯器窗格中獲取選定文本的起始和結束索引。
當我在編輯器窗格中選擇文本時,編輯器窗格返回的索引與實際HTML文件中字符串的索引不同。當我選擇單行文本時,兩個索引都是相同的,但如果我選擇多行,則索引是不同的。
我正在使用以下代碼。
int start = editorpane.getSelectionStart();
int end = editorpane.getSelectionEnd();
JOptionPane.showMessageDialog(null, "Start index "+ start);
JOptionPane.showMessageDialog(null, "End index "+end);
它返回的索引較小。
當使用下面的代碼它返回實際的索引,但我想在上面的代碼相同的索引。
try
{
File f1= new File("path of the file");
FileReader fin= new FileReader(f1);
BufferedReader br= new BufferedReader(fin);
String s=br.readLine();
String con="";
while(s!=null)
{
con=con+s;
s=br.readLine();
}
int l=con.lastIndexOf("CASE CITED");
System.out.println("index="+l);
}
catch(Exception ex){}
}
你在你的最後一個問題,你會「照顧」,並稱在需要大寫字母表示。這個問題沒有表明這一點,甚至更糟糕。 -1這兩個問題。 –
這個問題需要大寫的地方 –
試着看[編輯](http://stackoverflow.com/posts/16585264/revisions)! –