2016-03-02 45 views
0

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

回答

3

看看Concurrency in Swing的原因,当前的做法是行不通的,然后看看Listening for Changes on a DocumentImplementing a Document Filter对于一些可能的解决方案

正如有人必然会提到它,DON “T使用KeyListener,它不是问题

一个合适的解决方案将更加简单,Swing是单线程,事件驱动的框架。因此,阻止事件派发线程的任何事情都将阻止它处理新事件(包括绘画事件),从而导致UI无响应。

作为事件驱动的环境,您需要注册感兴趣的事件,以便在发生某些事件时收到通知这是Observer Pattern的示例),然后根据这些事件采取适当的措施。

但请记住,你无法通过DocumentListener进行更改Document,所以要小心有

+0

也做MadProgrammer建议,因为他知道他的摇摆。 –

+0

我很难读懂JTextField ..本质上我只是把JTextField放在我的JToolBar中,旁边有一个Button等等。当Button被按下时,它触发事件。我的问题是获取系统可读的JTextField我使用这个InputStream is = new ByteArrayInputStream(jt.getText()。getBytes(Charset.forName(“UTF-8”))); '但是现在已经被证明在同样的问题上失败了 – TravJav92

+0

考虑提供一个[可运行示例](https://stackoverflow.com/help/mcve),它可以说明你的问题。这不是代码转储,而是您正在做的事情的一个例子,它突出了您遇到的问题。这将导致更少的混淆和更好的回应 – MadProgrammer

3

你不想尝试删除控制台UI代码为事件驱动的GUI,因为它永远不会像那样工作。相反,您需要使用GUI事件来触发您的操作,而不是readln。

您必须决定的第一件事是您希望用来触发拼写检查的事件。对于我的钱,我会在JTextField中获取用户输入,而不是JTextArea,因为使用前者,我们可以通过在JTextField上添加一个ActionListener来轻松捕获按键键盘上的<enter>。你总是可以同时使用,然后一旦文本进行拼写检查,将其移动到JTextArea中,而这正是我建议:

  • 使用一个JTextField,
  • 添加一个ActionListener JTextField的每次按下领域已经集中,并进入通知,
  • 这个监听器内,提取JTextField中的文本,通过在现场呼吁getText()
  • 然后在提取的文本运行拼写检查代码,
  • 和将结果输出到附近的JTextArea中。
+0

当你谈到使用JTextField时,我有点需要使用JTextArea,因为应用程序的想法就像一个Word文档类型的应用程序,除非我误解了你。 – TravJav92