2012-11-02 62 views
1

我在JApplet中有一个带有ActionListener的JTextField。我使用Eclipse编译它,它工作正常。但是,当我尝试使用applet将它加载到.html文件中时,JTextField在按下时不会注册/识别ENTER键。它看起来像ActionListener没有工作。我用过:按ENTER键不适用于小程序中的JTextField

public void init() { 
    textField = new JTextField(20); 
    textField.setText("Enter your question here."); 
    textField.selectAll(); 
    textField.addActionListener(this); 

    textArea = new JTextArea(10, 20); 
    textArea.setEditable(false); 
    JScrollPane scrollPane = new JScrollPane(textArea, 
      JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

    // Add Components to the Applet. 
    GridBagLayout gridBag = new GridBagLayout(); 
    Container contentPane = getContentPane(); 
    contentPane.setLayout(gridBag); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.gridwidth = GridBagConstraints.REMAINDER; 

    c.fill = GridBagConstraints.HORIZONTAL; 
    gridBag.setConstraints(textField, c); 
    contentPane.add(textField); 

    c.fill = GridBagConstraints.BOTH; 
    c.weightx = 1.0; 
    c.weighty = 1.0; 
    gridBag.setConstraints(scrollPane, c); 

    contentPane.add(scrollPane); 

    newline = System.getProperty("line.separator"); 
} 

public void actionPerformed(ActionEvent event) { 
    String text = textField.getText(); 
    String question = ""; 
    String answer = ""; 

    question = textField.getText(); 
    question = ProcessString(question); 
    answer = Answer(question); 
    textArea.append(text + newline); 
    textArea.append(answer + newline); 
    textField.selectAll(); 
} 

static String noAnswer; 
static boolean knowAnswer = true; 

// process the question string, take out non-ACSII characters, spaces, to 
// lower space 
public String ProcessString(String question) { 
    question = question.toLowerCase(); 
    String[] words = question.split("\\s+"); 

    question = ""; 
    for (int wordCount = 0; wordCount < words.length; wordCount++) { 
     words[wordCount] = words[wordCount].replaceAll("[^A-Za-z]", ""); 
     if (wordCount != words.length - 1) 
      question = question + words[wordCount] + " "; 
     else 
      question = question + words[wordCount]; 
     // System.out.println(words[wordCount]); 
    } 
    return question; 
} 

public String Answer(String question) { 

    String answer = ""; 

    /* 
     if the database know the answer (did not not know), then return the 
     answer. if the database does not know the answer, then recover the 
     answer in database. 
    */ 
    if (knowAnswer == true) { 
     // open the database file and search if questions matches any one in 
     // the 
     // database 
     Scanner sc = null; 
     try { 
      sc = new Scanner(new File("database.txt")); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     int answerFrequency = 0; 

     boolean matchFound = false; 

     while (sc.hasNext()) { 

      int questionCount = sc.nextInt(); 
      String line = sc.nextLine(); 

      String[] databaseLine = line.split("\\s+"); 
      String databaseQuestion = ""; 
      String databaseAnswer = ""; 

      // collect words for database questions 
      for (int wordCount = 1; wordCount <= questionCount; wordCount++) { 
       if (wordCount != questionCount) 
        databaseQuestion = databaseQuestion 
          + databaseLine[wordCount] + " "; 
       else 
        databaseQuestion = databaseQuestion 
          + databaseLine[wordCount]; 
      } 

      // collect words for database answer 
      for (int wordCount = questionCount + 2; wordCount < databaseLine.length; wordCount++) { 
       databaseAnswer = databaseAnswer + databaseLine[wordCount] 
         + " "; 
      } 

      // if the question is found in database, print answer 
      if (question.equals(databaseQuestion)) { 
       matchFound = true; 

       // the current answer is more frequency than the previous 
       // found 
       // answer, reassign the current answer the find answer 
       if (answerFrequency < Integer 
         .parseInt(databaseLine[questionCount + 1])) { 
        answerFrequency = Integer 
          .parseInt(databaseLine[questionCount + 1]); 
        answer = databaseAnswer; 
       } 
      } 
     } 

     if (matchFound == true) { 
      // System.out.println(answer); 
      knowAnswer = true; 
     } else { 
      // System.out.println("I don't know what you mean. How should I answer your question?"); 
      knowAnswer = false; 
      noAnswer = question; 
      answer = "I don't know how to respond. How should I answer that?"; 
     } 

     sc.close(); 
    } else if (knowAnswer == false) { 
     String[] word = noAnswer.split(" "); 

     BufferedWriter writer = null; 
     answer = question; 
     try { 
      writer = new BufferedWriter(
        new FileWriter("database.txt", true)); 
      writer.newLine(); 
      writer.write(word.length + " " + noAnswer + " 1 " + answer); 
     } catch (IOException e) { 
     } finally { 
      try { 
       if (writer != null) 
        writer.close(); 
      } catch (IOException e) { 
       System.out.println("Cannot write to file"); 
      } 
     } 
     answer = "I got that."; 
     knowAnswer = true; 
    } 
    return answer; 
} 

真的很感谢帮助。

+0

适合我的工作,有更多的来源? – MadProgrammer

回答

1

这似乎为我工作....

public class TestApplet04 extends JApplet implements ActionListener { 

    private JTextField textField; 
    private JTextArea textArea; 
    private String newline; 

    public void init() { 
     textField = new JTextField(20); 
     textField.setText("Enter your question here."); 
     textField.selectAll(); 
     textField.addActionListener(this); 

     textArea = new JTextArea(10, 20); 
     textArea.setEditable(false); 
     JScrollPane scrollPane = new JScrollPane(textArea, 
       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
       JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

     // Add Components to the Applet. 
     GridBagLayout gridBag = new GridBagLayout(); 
     Container contentPane = getContentPane(); 
     contentPane.setLayout(gridBag); 
     GridBagConstraints c = new GridBagConstraints(); 
     c.gridwidth = GridBagConstraints.REMAINDER; 

     c.fill = GridBagConstraints.HORIZONTAL; 
     gridBag.setConstraints(textField, c); 
     contentPane.add(textField); 

     c.fill = GridBagConstraints.BOTH; 
     c.weightx = 1.0; 
     c.weighty = 1.0; 
     gridBag.setConstraints(scrollPane, c); 

     contentPane.add(scrollPane); 

     newline = System.getProperty("line.separator"); 
    } 

    public void actionPerformed(ActionEvent event) { 
     String text = textField.getText(); 
     String question = ""; 
     String answer = ""; 

     question = textField.getText(); 
//  question = ProcessString(question); 
//  answer = Answer(question); 
     textArea.append(text + newline); 
     textArea.append(answer + newline); 
     textField.selectAll(); 
    } 
} 

反馈后更新

这里是你主要的问题......

sc = new Scanner(new File("database.txt")); 

这将导致你一些问题。首先,applet不太可能拥有从客户端机器读取的访问权限,所以您可能会遇到安全异常。其次,正如前面的陈述可能表明的那样,该文件不可能存在于客户端机器上。

您需要将此资源嵌入到应用程序的Jar中并使用getClass().getResource("...")才能访问它。这将返回一个URL,从中您可以使用URL#openConnection访问可用于扫描仪的InputStream

+0

它仍然不适用于我。如果我拿出ProcessString();和Answer();函数,然后代码工作。但是如果我将它们放在同一个JApplet类中,那么它不起作用。即使我拿出函数并把它们放在一个单独的类中(并调用类函数),它仍然不起作用。所以基本上,调用这些函数使得它不能在.html applet中工作。 – yangliu2

+0

S,我们需要看到ProcessString&Answer方法来了解更多 – MadProgrammer

+0

这有点麻烦,但在这里。我试图读取文本文件和/或写入这些函数中的文本文件。我贴完了。 – yangliu2

1

尝试在ActionMap中添加一个Action并将ENTERMap设置为ENTER键。

textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter"); 
textField.getActionMap().put("enter", new AbstractAction() {...}); 

该字段必须具有焦点。

+0

对不起,我应该把代码放在public void init()中吗?它“无法解决KeyStroke”。 – yangliu2

+0

@ user1793092'import javax.swing.KeyStroke;' – Mordechai

0

我也是Java的新手,但发现JTextfield只是一行代码。 如果您想要多行输入,则需要改为使用JTextArea

希望这会有所帮助:)