English 中文(简体)
如何使用 JTextPhextPanel 获得字符串
原标题:How to use a JTextPanel to get a string

我需要使用 JtextPanel 从键盘上获取输入, 在我按下输入时将其保存在字符串上, 然后用该字符串来根据输入( 例如“ help” 或“ quit ” ) 给出的行执行某些动作。 我在我的 KeyListener 在 JTextPanel 的 KeyListener 中得到了这个 :

...
public void keyPressed(KeyEvent e) {
     int key = e.getKeyCode();    

     if (key == KeyEvent.VK_ENTER) {
        inputString = textField.getText();
        textArea.append(inputString + "
");
        textField.setText("");

        }
}
....

但我不能直接调用这个方法

String input = processInput();
    if((input).equals("help"))
          ............
    else if ((input).equals("go"))
          ............

and processInput should be a method that waits for the (key== KeyEvent.VK_ENTER), like happens when you use the scanf in C or the bufferedReader in java, it waits for you giving a string from keyboard till you press enter. EDIT

我的应用程序管理这样的命令

while(!finished) {

    finished = processInput() 
}

processInput manages the command given in input. That s why I cant call processInput() from the keyListener I hope i was clear, my english is so bad!

感谢 谢谢

最佳回答

这个方法如何? 很简单

密钥收听器 :

...
public void keyPressed(KeyEvent e) {
     int key = e.getKeyCode();    

     if (key == KeyEvent.VK_ENTER) {
        inputString = textField.getText();
        textArea.append(inputString + "
");
        textField.setText("");
        processInput(inputString); //crunch it
        }
}
....

和其他地方

public void processInput(String input) {
    if((input).equals("help"))
          ............
    else if ((input).equals("go"))
          ............
}
问题回答

我相信你被困在 事件驱动界面的建筑设计上

这里的想法是, 您不会去“ 等待” 输入或其它东西 。 您设置了接口, 附加密钥监听器( 您确实在某处有一个 < code > addKeyListener () () < ) < right... ), 然后重做 。 您放弃控制流, 让主方法结束 。

当用户做一些值得注意的事情时, 您可以处理它, 所以说您有一种方法 < code> processText( string text) , 您会在那里的按键听器中说 < code> processText( inputString);

因此,当用户输入某种东西并点击输入时,它开始在密钥“听器”中执行,该“听器”将控制流传送到 processText () 方法中,该方法将尽一切可能,因为该文本的存在。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签