問題:試圖得到儘可能下面只JTextArea中的代碼相同的效果,所以我想讀的JTextArea中和被推薦拼寫建議每次用戶鍵入一個新的拼寫錯誤的單詞。讀的JTextArea對於爵士拼寫檢查器API
下面是'System.in'的工作示例,它運行良好。
商(VAR userField =的JTextArea & dic.txt是系統使用的建議,英語語言的列表)
碼(1)
public SpellCheckExample() {
try {
SpellDictionary dictionary = new SpellDictionaryHashMap(new File(dic.txt));
spellCheck = new SpellChecker(dictionary);
spellCheck.addSpellCheckListener(this);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.print("Enter text to spell check: ");
String line = in.readLine();
if (line.length() <= 0)
break;
spellCheck.checkSpelling(new StringWordTokenizer(line));
}
} catch (Exception e) {
e.printStackTrace();
}
}
我一直在努力:
CODE(2)
public void spellChecker() throws IOException{
String userName = System.getProperty("user.home");
SpellDictionary dictionary = new SpellDictionaryHashMap(new File(userName+"/NetBeansProjects/"+"/project/src/dic.txt"));
SpellChecker spellCheck = new SpellChecker(dictionary);
spellCheck.addSpellCheckListener(this);
try{
StringReader sr = new StringReader(userField.getText());
BufferedReader br = new BufferedReader(sr);
while(true){
String line = br.readLine();
if(line.length()<=0)
break;
spellCheck.checkSpelling(new StringWordTokenizer(line));
}
}catch(IOException e){
e.printStackTrace();
}
}
2016年3月3日(Updat E)
public void spellChecker() throws IOException{
// getting context from my dic.txt file for the suggestions etc.
SpellDictionary dictionary = new SpellDictionaryHashMap(new File("/Users/myname/NetBeansProjects/LifeSaver/src/dic.txt"));
SpellChecker spellCheck = new SpellChecker(dictionary);
// jt = JTextField already defined in constructors and attemtpting to pass this into system and
InputStream is = new ByteArrayInputStream(jt.getText().getBytes(Charset.forName("UTF-8")));
//spellCheck.checkSpelling(new StringWordTokenizer(line)); ""ORIGINAL"""
// reccomending cast to wordfinder
spellCheck.checkSpelling(new StringWordTokenizer(is);
}
也做MadProgrammer建議,因爲他知道他的搖擺。 –
我很難讀懂JTextField ..本質上我只是把JTextField放在我的JToolBar中,旁邊有一個Button等等。當Button被按下時,它觸發事件。我的問題是獲取系統可讀的JTextField我使用這個InputStream is = new ByteArrayInputStream(jt.getText()。getBytes(Charset.forName(「UTF-8」))); '但是現在已經被證明在同樣的問題上失敗了 – TravJav92
考慮提供一個[可運行示例](https://stackoverflow.com/help/mcve),它可以說明你的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的混淆和更好的迴應 – MadProgrammer