Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.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
KeyListener在Java中工作不正常_Java_Keylistener - Fatal编程技术网

KeyListener在Java中工作不正常

KeyListener在Java中工作不正常,java,keylistener,Java,Keylistener,我有一个关于Java的问题,它与用于监听键的类KeyListener有关。在我的应用程序中,我首先用JavaFXtoolkit显示一个介绍视频。我为视频使用的JFXPanel设置了一个KeyListener,它工作正常,可以在播放视频时读取我在键盘上按下的键。但问题是,当视频结束时(因为它结束播放,或者因为我通过按“Escape”键中断线程来停止播放),我试图向应用程序的主JFrame中添加一个新的KeyListener,但我无法从中读取任何键 您还应该问问自己,为什么我在JFXPanel中添加

我有一个关于Java的问题,它与用于监听键的类
KeyListener
有关。在我的应用程序中,我首先用
JavaFX
toolkit显示一个介绍视频。我为视频使用的
JFXPanel
设置了一个
KeyListener
,它工作正常,可以在播放视频时读取我在键盘上按下的键。但问题是,当视频结束时(因为它结束播放,或者因为我通过按“Escape”键中断线程来停止播放),我试图向应用程序的主
JFrame
中添加一个新的
KeyListener
,但我无法从中读取任何键

您还应该问问自己,为什么我在
JFXPanel
中添加了一个
KeyListener
,然后在视频结束后,我在应用程序的主
JFrame
中添加了一个新的
KeyListener
,而不是从一开始就只在
JFrame
中添加一个
KeyListener
。原因是,如果我这样做,那么在播放视频时我无法读取任何键,因此有人建议我将
keylister
添加到
JFXPanel

以下是我申请的所有代码:

/**
 * Main class of the application.
 */
public class Main{

  // Define the variable for the window of the game.
  public static JFrame window;

  // Define the variable for the introductory video.
  public static MediaPlayer video;

  // Define the variable for a thread.
  public static Thread thread;

  /**
   * Main function of the application.
   */
  public static void main(String[] args){

    // Create a Swing thread.
    SwingUtilities.invokeLater(new Runnable(){

      @Override
      public void run(){

        // Prevent the JavaFX toolkit from closing.
        Platform.setImplicitExit(false);

        // Create the window of the game.
        window = new JFrame();

        // Set the title.
        window.setTitle("Chip");

        // Set the resolution as 1920 x 1280.
        window.setSize(1926,1343);

        // Set the location as in the middle of the screen.
        window.setLocationRelativeTo(null);

        // Set the operation when the window closes.
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Disable the maximization and resizable mode.
        window.setResizable(false);

        // Show the window.
        window.setVisible(true);

        // Create a key listener.
        KeyListener keyListener = createKeyListener();

        // Create a JavaFX panel.
        JFXPanel panelJavaFX = new JFXPanel();

        // Add the key listener to the JavaFX panel.
        panelJavaFX.addKeyListener(keyListener);

        // Create a JavaFX thread.
        Platform.runLater(new Runnable(){

          @Override
          public void run(){

            // Create a new thread.
            thread = new Thread(new Runnable(){

              public void run(){

                try{

                  // Show the introductory video.
                  showVideo(panelJavaFX);

                  // Pause the execution of the application for 30 seconds (duration of the introductory video).
                  Thread.sleep(30000);

                }catch (InterruptedException interruptedException){

                  // Stop the video if an interruption has been occurred.
                  video.stop();

                }

                // Set the background image.
                String filename = "./media/image/background.jpg";
                window.setContentPane(new JLabel(new ImageIcon(filename)));

                // Show the window.
                window.setVisible(true);

                // Add the key listener to the window of the game.
                window.addKeyListener(keyListener);

              }

            });

            // Start the execution of the thread.
            thread.start();

          }

        });

      }

    });

  }

  /**
   * Creates a key listener.
   * @return Key listener.
   */
  public static KeyListener createKeyListener(){

    // Create the key listener.
    KeyListener keyListener = new KeyListener(){

      // Set the behavior whenever a key is pressed.
      @Override
      public void keyPressed(KeyEvent keyEvent){

        // Check if the "Escape" key is pressed.
        if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){

          // Check if the introductory video it is being played.
          if (video.getStatus().equals(Status.PLAYING)){

            // Make an interruption in the thread that is being executed.
            thread.interrupt();

          }

        }

      }

      // Set the behavior whenever a key is released.
      @Override
      public void keyReleased(KeyEvent keyEvent){}

      // Set the behavior whenever a key is typed.
      @Override
      public void keyTyped(KeyEvent keyEvent){}

    };

    // Return the key listener.
    return keyListener;

  }

  /**
   * Shows the introductory video.
   * @param panelJavaFX JFXPanel used to display the video.
   */
  public static void showVideo(JFXPanel panelJavaFX){

    // Set the size of the JaxaFX panel as the resolution of the introductory video (1920 x 1080).
    panelJavaFX.setSize(1920,1080);

    // Set the location of the JavaFX panel as in the middle of the window of the game.
    int coordinateX = (window.getWidth() - panelJavaFX.getWidth() - window.getInsets().left - window.getInsets().right) / 2;
    int coordinateY = (window.getHeight() - panelJavaFX.getHeight() - window.getInsets().top - window.getInsets().bottom) / 2;
    panelJavaFX.setLocation(coordinateX,coordinateY);

    // Define the video file.
    String filename = "./media/video/introduction.mp4";
    video = new MediaPlayer(new Media(new File(filename).toURI().toString()));

    // Add the video to the JavaFX panel.
    panelJavaFX.setScene(new Scene(new Group(new MediaView(video))));

    // Add the JavaFX panel to the window of the game.
    window.getContentPane().setLayout(null);
    window.add(panelJavaFX);

    // Play the video.
    video.setAutoPlay(true);

  }

}

这个问题的正确答案是:


我在指令
窗口之前添加了指令
window.requestFocus()
。addKeyListener(keyListener)
,现在它可以在播放视频后按下“Escape”键时读取。

导入,由于缩进良好,每个方法的所有注释对所有人都没有用处;)@azro-嗯,至少这个输入应该编译。。。并且应该可以很容易地生成一个可运行的程序。我见过更糟的。比这糟糕100倍。在过去的几个小时里大概有100次。@GhostCat我知道我知道对不起,我是新来的。您会推荐我使用代码的哪些特性?我的意思是,在我的问题中,我应该删除导入,以及哪些注释?谢谢。可能有两份