我需要使用JtextPanel从键盘获取输入,并在按Enter键时将其保存在字符串中,然后使用该字符串根据输入中给定的行执行某些操作(例如“帮助”或“退出”)。我在我的KeyListener为JTextPanel得到了这个:如何使用JTextPanel获取字符串
...
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
inputString = textField.getText();
textArea.append(inputString + "\n");
textField.setText("");
}
}
....
,但我不能直接调用这个方法。我需要这样的东西
String input = processInput();
if((input).equals("help"))
............
else if ((input).equals("go"))
............
和processInput应该是等待(键== KeyEvent.VK_ENTER)的方法,就像当你使用在C scanf或Java中的BufferedReader发生,它会等待你从键盘输入一个字符串,直到按下回车键。 编辑
我的应用程序来管理这样的
while(!finished) {
finished = processInput()
}
processInput管理中输入指定的程序指令。这就是为什么我不能从keyListener调用processInput() 我希望我很清楚,我的英语非常糟糕!
谢谢
就叫'processInput(inputString);'当你按下回车键。 – nullpotent