Java 获得对 JPanel 的关注

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2135223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:58:43  来源:igfitidea点击:

Obtaining focus on a JPanel

javaswingfocusjpanel

提问by Vlad T.

I have a JPanelinside a JFrame. I have registered a KeyListener, based on which I want to update the JPanel. The problem I am having is that I cannot get the focus on the JPaneland therefore my KeyListenerwon't work. I already know that the KeyListeneris functional because I registered it with the JFrameand it worked fine. My code goes something like this at the moment:

我有一个JPanel里面的JFrame. 我已经注册了一个KeyListener,我想根据它更新JPanel. 我遇到的问题是我无法集中注意力JPanel,因此我KeyListener无法工作。我已经知道KeyListener是功能性的,因为我在 中注册了它JFrame并且它运行良好。我的代码现在是这样的:

myFrame.setFocusable(false);
myPanel.setFocusable(true);
myPanel.addKeyListener(myKL);
myFrame.add(myPanel);

Has anyone encountered a problem like this before? Is there something I am missing in regards to this?

有没有人遇到过这样的问题?在这方面我有什么遗漏吗?

P.S.: I do not have any components inside the JPanelI just draw an Image on the background, so I need the focus to be on the JPanel itself and not on something inside it.

PS:我里面没有任何组件,JPanel我只是在背景上绘制一个图像,所以我需要把焦点放在 JPanel 本身而不是它里面的东西上。

采纳答案by David Koelle

Although you're indicating that the panel can be focusable, the panel isn't asking for focus. Try using myPanel.requestFocus();.

尽管您表示该面板可以聚焦,但该面板并未要求聚焦。尝试使用myPanel.requestFocus();.

回答by Uri

I sometimes face a similar problem. I've noticed that in some cases it is better to make or request focus on a specific control within the panel that is within the frame (e.g., the input box to which you want keyboard input to go), rather than request focus for the pane itself.

我有时会遇到类似的问题。我注意到,在某些情况下,最好是在框架内的面板(例如,您希望键盘输入的输入框)内的特定控件上设置或请求焦点,而不是请求焦点窗格本身。

回答by John Doe

Try

尝试

panel.setFocusable(true);
panel.setRequestFocusEnabled(true);

// some code here

panel.grabFocus();

回答by Lawrence Dol

Use setFocusable(true)and then requestFocusInWindow(). But the latter must be done afterthe window containing the panel is made visible, for which you will likely need to register a window listener and do the requestFocusInWindow()in the window activated handler code.

使用setFocusable(true)然后requestFocusInWindow()。但后者必须做到变得可见含面板窗口中,你可能需要用它来注册一个窗口监听器并执行requestFocusInWindow()窗口激活处理程序代码。

Note: Specifically after the window isvisible, not just after calling setVisible(true).

注:后具体的窗口可见的,不只是打电话后setVisible(true)

回答by Bram Janssens

Try something like this:

尝试这样的事情:

    myFrame.addFocusListener(new FocusAdapter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void focusGained(FocusEvent aE) {
            myPanel.requestFocusInWindow();
        }
    });