嗨,看看这段代码。我的目标是让我的应用程序在我输入 textarea 中列出的单词之一时做出反应slowa[]
。正如你所看到的,我创建了inputMethodListner
但是当我输入它正在工作的任何单词时。我尝试进行调试打印以查看发生了什么,我发现既没有方法inputMethodTextChanged()
也没有inputMethodTextChanged()
被调用一次:(我做错了什么?
import java.awt.*;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import javax.swing.*;
public class BrzydkieSlowa extends JFrame {
static String[] slowa = {"shit", "fuck"};
private BrzydkieSlowa(){
//Create and set up the window.
JFrame frame = new JFrame("Brzydkie slowa");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextArea textArea1 = new JTextArea(10,10);
textArea1.addInputMethodListener(new InputMethodListener() {
@Override
public void caretPositionChanged(InputMethodEvent arg0) {
int brzydkie = 0;
int i = 0;
while(brzydkie == 1 || i > 1){
if(textArea1.getText().compareTo(slowa[i])== 0)
brzydkie = 0;
i++;
}
if(brzydkie == 1)
JOptionPane.showMessageDialog(null, "brzydkie slowo");
}
@Override
public void inputMethodTextChanged(InputMethodEvent event) {
// TODO Auto-generated method stub
}
});
frame.getContentPane().add(textArea1, BorderLayout.CENTER);
//Display the window.
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new BrzydkieSlowa();
}
}