下面的代码是Java中基于wordnet的程序的一部分,下面的事件在我所拥有的GUI上单击按钮时起作用。基本上使用wordnet,你搜索一个单词,然后当它找到单词时,它会显示该单词的所有含义。由于我一直在使用Netbeans,“System.out.println”在其终端上打印出完全正确的单词的所有含义,但在“jTextField2”的GUI上(我希望它打印其含义)它打印最后一个而不是全部。另一个奇怪的事情是,当我希望输出从文本字段的顶部向下到底部(如果适用)时,它会将最后一个打印到文本字段的中间。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
JWNL.initialize(new FileInputStream("C:\\jwnl14-rc2\\config\\file_properties.xml"));
Dictionary wordnet = Dictionary.getInstance();
String text = jTextField1.getText().trim(); //the word is inputted in jTextField1
text = text.replace(' ','_');
jTextField2.setText("Searches for " + text + ":");
IndexWord verb = wordnet.getIndexWord(POS.NOUN, text);
Synset[] verbsenses = verb.getSenses();
for (Synset verbsense : verbsenses)
{
System.out.println(verb + ": " + verbsense.getGloss()); //displays every output in the terminal line
jTextField2.setText(verb + ": " + verbsense.getGloss()); //only displays the last output from a particular word
}
} catch (JWNLException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
如果有人能提供帮助,那将非常感激。
当您调用jTextField2.setText(...)时,您将使用新文本替换该字段中存在的文本。
JTextField可能不是你想要的 - 它只允许一行文本。你应该看看JTextArea。