Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在方法中使用KeyEvent_Java_Swing_Keyevent - Fatal编程技术网

Java 在方法中使用KeyEvent

Java 在方法中使用KeyEvent,java,swing,keyevent,Java,Swing,Keyevent,如果我使用的是GUI,并且有一个用户将输入的文本字段,然后程序将返回该字段,那么在方法期间我将如何访问KeyEvent?(按键事件是当按下Enter键->文本字段中的文本将生成响应时) 例如:如果程序(通过一种方法)问用户“你想吃这个蛋糕吗?”然后用户会在文本字段中输入“是”或“否”,根据回答,程序会用另一种方法给出另一个问题或情况 伪代码: public void cakeQuestion(){ eventList.setText(eventList.getText() + "

如果我使用的是GUI,并且有一个用户将输入的文本字段,然后程序将返回该字段,那么在方法期间我将如何访问KeyEvent?(按键事件是当按下Enter键->文本字段中的文本将生成响应时)

例如:如果程序(通过一种方法)问用户“你想吃这个蛋糕吗?”然后用户会在文本字段中输入“是”或“否”,根据回答,程序会用另一种方法给出另一个问题或情况

伪代码:

public void cakeQuestion(){
        eventList.setText(eventList.getText() + "\nWould You Like To Eat This Cake?"); //eventList is a textArea 
       //***KeyEvent takes place, perhaps saving the user's input as a String called resposne
           if(response.equals("yes"){
              eatCake //eatCake is another method with another situation
           }         
           else if(response.equals("no"){
              eatPie //eatPie is another method with another situation
           } 
           else{eventList.setText(eventList.getText() + "\nI don't understand that response");}
     }

解决方案:不使用KeyEvent。如果您在JTextField中等待enter键,只需给该字段一个ActionListener,它就会在enter键按下时做出响应

myTextField.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String response = e.getActionCommand();
        if(response.equals("yes"){
            eatCake(); //eatCake is another method with another situation
        }         
        else if(response.equals("no"){
            eatPie(); //eatPie is another method with another situation
        } else{
            eventList.setText(eventList.getText() + "\nI don't understand that response");
        }
    }
});
边位:

  • 如果您希望GUI只保留有限数量的定义良好的条目,如“是”和“否”,那么不要使用JTextField,而是使用更适合于受控输入的东西,如JRadioButtons(添加到ButtonGroup)、JSpinner或JComboBox。与其警告用户他们的输入不正确,不如先不要让他们输入错误的输入
  • 如果要响应文本组件中的按键(例如,JTextField、JTextArea…),请将DocumentListener添加到文本组件的文档中
  • 如果要筛选输入的文本,例如,检查文本的有效性,如果无效,则不允许在字段中使用,然后将DocumentFilter添加到文本组件的文档中

    • 解决方案:您不使用KeyEvent。如果您在JTextField中等待enter键,只需给该字段一个ActionListener,它就会在enter键按下时做出响应

      myTextField.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              String response = e.getActionCommand();
              if(response.equals("yes"){
                  eatCake(); //eatCake is another method with another situation
              }         
              else if(response.equals("no"){
                  eatPie(); //eatPie is another method with another situation
              } else{
                  eventList.setText(eventList.getText() + "\nI don't understand that response");
              }
          }
      });
      
      边位:

      • 如果您希望GUI只保留有限数量的定义良好的条目,如“是”和“否”,那么不要使用JTextField,而是使用更适合于受控输入的东西,如JRadioButtons(添加到ButtonGroup)、JSpinner或JComboBox。与其警告用户他们的输入不正确,不如先不要让他们输入错误的输入
      • 如果要响应文本组件中的按键(例如,JTextField、JTextArea…),请将DocumentListener添加到文本组件的文档中
      • 如果要筛选输入的文本,例如,检查文本的有效性,如果无效,则不允许在字段中使用,然后将DocumentFilter添加到文本组件的文档中

      @nulsaint:的确,这是一个很好的答案。1+@nulssaint:的确,这是一个很好的答案。1+谢谢你!那么,为了添加更多的方法,我只需添加更多的
      @覆盖
      以及我想要的任何方法?@AtticusTrebmal:我不明白你的评论--为什么添加更多的方法?给ActionListener?不,你不会想这么做的。那么,我如何添加另一个方法,比如actionPerformed方法呢?就像我想用不同的文本使用相同的方法一样?@AtticusTrebmal:我又一次感到困惑了。如果您想在另一个JTextField中添加类似的内容,您可以执行与上面相同的操作。如果方法相同,那么创建一个ActionListener变量并将相同的侦听器添加到两个JTextFields。我想将类似的方法添加到相同的JTextField谢谢!那么,为了添加更多的方法,我只需添加更多的
      @覆盖
      以及我想要的任何方法?@AtticusTrebmal:我不明白你的评论--为什么添加更多的方法?给ActionListener?不,你不会想这么做的。那么,我如何添加另一个方法,比如actionPerformed方法呢?就像我想用不同的文本使用相同的方法一样?@AtticusTrebmal:我又一次感到困惑了。如果您想在另一个JTextField中添加类似的内容,您可以执行与上面相同的操作。如果方法相同,则创建一个ActionListener变量,并将相同的侦听器添加到两个JTextFields