问题:试图得到尽可能下面只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