将Keylistener添加到全屏JWindow

| 我已经制作了一个全屏的JWindow,并且我想添加一个简单的KeyListener,以便在按下箭头键的情况下执行某些操作 但是我不知道为什么它不起作用。我已将keylistener添加到所有组件。但是它没有用 谁知道这是什么问题?     
已邀请:
默认情况下,除非在创建窗口时将JFrame指定为所有者,否则JWindow不会接收键事件。以下代码演示了这一点:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class WindowTest
{
    public static void main(String[] args)
    {

        JFrame frame = new JFrame();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//      frame.setLocation(-200, 0); // uncomment this line to hide the dummy frame
        frame.setVisible( true );

        JWindow window = new JWindow(); // this doesn\'t work
//      JWindow window = new JWindow(frame); // this works

        window.getContentPane().add( new JTextField(10), BorderLayout.NORTH );
        window.getContentPane().add( new JButton(\"Button\") );
        String[] items = { \"Select Item\", \"Color\", \"Shape\", \"Fruit\" };
        JComboBox mainComboBox = new JComboBox( items );
        window.getContentPane().add( mainComboBox, BorderLayout.SOUTH );

        window.setBounds(50, 50, 200, 200);
        window.setVisible(true);
        window.getRootPane().setBorder(new javax.swing.border.MatteBorder(4, 4, 4, 4, Color.BLUE));
     }
}
一个更简单的解决方案是使用未修饰的JFrame:
JFrame frame = new JFrame();
frame.setUndecorated(true);
  并且我想添加一个简单的KeyListener,以防在按下箭头键时执行某些操作 另外,您不应为此使用KeyListener。您应该使用按键绑定。     
可能只是将相关组件设置为可聚焦。例如。
myContentPane.setFocusable(true);
    

要回复问题请先登录注册