Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.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 - Fatal编程技术网

Java中理解KeyEvent的问题

Java中理解KeyEvent的问题,java,Java,我目前正在尝试制作一个程序,其中输入了三个按钮,每次都有不同的事情发生 我知道我必须使用按键,但这确实让我感到困惑,因为当我运行程序时,它不会等待我输入任何内容 我一直在遵循在线指南,因为我对编程基本上是新手,所以如果你有更好的方法一起做,那么请说出来 import java.awt.event.KeyEvent; public class Trial { public static void main(String[] args) { System.out.println("W

我目前正在尝试制作一个程序,其中输入了三个按钮,每次都有不同的事情发生

我知道我必须使用
按键
,但这确实让我感到困惑,因为当我运行程序时,它不会等待我输入任何内容

我一直在遵循在线指南,因为我对编程基本上是新手,所以如果你有更好的方法一起做,那么请说出来

import java.awt.event.KeyEvent;

public class Trial {

  public static void main(String[] args) {
    System.out.println("Welcome to the Medical Registration Form program.");
    System.out.println("To enter a new patient's details, press 'N'");
    System.out.println("To access an existing pateient's details, press 'S'");
    System.out.println("To see all patient deatils currently saved, press 'P'");
  }

  public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_S) {
      System.out.println("You pressed a valid button");  
    } else {
      System.out.println("You pressed a bad button!");
      e.consume();
    }
  }
}

如果要使用控制台,请遵循以下代码片段:

public class Demo{

public static void main(String[] args) {
    // TODO code application logic here

    System.out.println("Welcome to the Medical Registration Form program.");
    System.out.println("To enter a new patient's details, press 'N'");
    System.out.println("To access an existing pateient's details, press 'S'");
    System.out.println("To see all patient deatils currently saved, press 'P'");
    Scanner scan = new Scanner(System.in);
    String input = scan.next();

    if(input.matches("S")){
        System.out.println("You pressed a valid button");  
    } else {
        System.out.println("You pressed a bad button!");

    }
}
否则,从Jframe扩展并实现KeyListener,如下所示:

public class Demo extends JFrame implements KeyListener{

public Demo(){

    this.addKeyListener(this);
}

public static void main(String[] args) {
    // TODO code application logic here

    System.out.println("Welcome to the Medical Registration Form program.");
    System.out.println("To enter a new patient's details, press 'N'");
    System.out.println("To access an existing pateient's details, press 'S'");
    System.out.println("To see all patient deatils currently saved, press 'P'");
    Demo demo = new Demo();
}   

@Override
public void keyPressed(KeyEvent e) {

    int keyCode = e.getKeyCode();
    if(keyCode == KeyEvent.VK_S){
        System.out.println("You pressed a valid button");  
    } else {
        System.out.println("You pressed a bad button!");
        e.consume();
    }
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

您还没有添加一个
KeyListener
,也没有添加一个框架来捕获任何关键事件。哪个魔法应该触发该事件并调用您的处理程序?您需要创建一个UI控件,并为其关键事件添加侦听器。如果你不需要UI(你正在做控制台应用程序),你可以使用例如。是的,我正在做一个控制台应用程序。但干杯,我会看看扫描仪和键盘监听器;class
Demo
不调用任何JFrame方法,比如它的构造函数或设置大小。@每次发生关键事件时都会调用SteveSmith方法,是的setSize和setVisible(true)。非常感谢@枕头123欢迎光临。如果它解决了你的问题,检查它作为答案。