简体   繁体   English

摇摆键盘没有响应

[英]Swing keyboard not responding

I'm using a KeyListener on a JFrame object which I set as FullScreenWindow, something like this code: 我在JFrame对象上使用KeyListener,我将其设置为FullScreenWindow,类似于以下代码:

class Game{

private GraphicsDevice device;
...

public void run(){
JFrame frame = new JFrame();
frame.addKeyListener(new MarioKeyListener());
device.setFullScreenWindow(frame);
}

...
}

And it works fine if I just create a Game object in my main method and call run(). 如果我只是在main方法中创建一个Game对象并调用run(),它就可以正常工作。 However I want to do this inside the mousePressed() function of a MouseAdapter which I added to another JFrame-s MenuItem. 但是我想在我添加到另一个JFrame-s MenuItem的MouseAdapter的mousePressed()函数中执行此操作。 The result is that the program runs as normal but doesn't accept any keyboard input. 结果是程序正常运行但不接受任何键盘输入。

JMenu gamemenu = new JMenu("Game");
   JMenuItem newGame = new JMenuItem("New Game");
    newGame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e){
    Game g = new Game();
    g.run();    
    }               
});
gamemenu.add(newGame);

I think My frame object is not in focus, but calling setFocusable(true) and requestfocusinwindow() did not help. 我认为我的框架对象不在焦点,但调用setFocusable(true)和requestfocusinwindow()没有帮助。 If anyone knows whats wrong or how to fix this, help would be greatly appreciated... 如果有人知道什么是错的或如何解决这个问题,将非常感谢帮助......

Tomi 托米

requestFocusInWindow() .. requestFocusInWindow() ..

Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window. 如果此Component的顶级祖先已经是焦点窗口请求此Component获取输入焦点

Are you checking the return value? 你在检查返回值吗? I suspect it is failing because the new window is not the focused component at the moment the method is called. 我怀疑它失败了,因为在调用方法时新窗口不是焦点组件。

If that is the case, the answer might be found in similar fashion to the dialog focus strategy of adding a RequestFocusListener to the mix. 如果是这种情况,则可以以与向混合添加RequestFocusListener对话焦点策略类似的方式找到答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM