正常工作,我有關於Java與用於鍵收聽類KeyListener
相關的問題。在我的應用程序中,我首先用JavaFX
工具包展示了一個介紹性視頻。我設置用於視頻JFXPanel
一個KeyListener
,它做工精細,它能夠讀取我在鍵盤按下,而視頻正在播放的按鍵。但問題是,當影片結束(可能是因爲它完成播放,因爲我通過使中斷線程按「逃生」鍵停止),我嘗試將新KeyListener
添加到應用程序的主JFrame
和我無法從中讀取任何密鑰。的KeyListener沒有在Java中
你也應該問自己,爲什麼我添加了一個KeyListener
到JFXPanel
,然後視頻結束後我添加了一個新的KeyListener
到應用程序的主JFrame
,而不是隻有一個KeyListener
從一開始就加入到JFrame
。原因是,如果我這樣做,那麼我無法讀取任何按鍵,而在視頻播放,所以有人建議我加入KeyListener
到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);
}
}
進口,在每個方法的所有意見是完全沒有有用的,由得益於已經縮進好;) – azro
@azro - 好吧,至少這個輸入應該編譯...並應導致一個可運行的程序很容易。我看到更糟。差100倍。在過去的幾個小時裏可能會有100次。 – GhostCat
@GhostCat我知道我知道 – azro