全屏窗口不会使用KeyListener或KeyBoardFocusManager获取键盘输入

时间:2012-06-24 16:00:41

标签: java swing fullscreen keylistener key-bindings

我在让KeyBoardFocusManger使用全屏Window时遇到问题。无论如何,它都不会得到键盘输入。我使用System.exit(0)println()来查找对keypressed / release / typed方法的任何调用,但不会抛出任何错误。我试过KeyListeners;但在我阅读this后,我改为KeyboardFocusManager,同样的事情仍然发生。我真的很绝望;根据我的判断,Window没有得到键盘的焦点?

这是我的主要内容:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            // Determine if full-screen mode is supported directly
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            if (gs.isFullScreenSupported()) {
                Frame frame = new Frame(gs.getDefaultConfiguration());
                SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(frame);
                // Enter full-screen mode
                gs.setFullScreenWindow(spaceInvaderUI);
            } else {
                JOptionPane.showMessageDialog(null, "Does not support full screen!", "Error 0x01", JOptionPane.ERROR_MESSAGE);
                System.exit(1);
            }
        }
    });
}

以下是包含KeyBoardFocusManger的用户界面,并在addListeners()方法中添加:

class SpaceInvaderUI extends Window {

    private JPanel drawingPanel;
    private Image background;
    private JButton btnExit;

    public SpaceInvaderUI(Frame frame) {
        super(frame);
        try {
            background = ImageIO.read(getClass().getResourceAsStream("background.png"));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
            System.exit(2);
        }
        createWindow();
    }

    private void createComponents() throws HeadlessException {
        drawingPanel = new DrawingPanel(background, this);
        btnExit = new JButton("Exit");
    }

    private void createWindow() {
        createComponents();
        addListeners();
        addComponentsToWindow();
    }

    private void addComponentsToWindow() {
        add(drawingPanel, BorderLayout.CENTER);
        add(btnExit, BorderLayout.SOUTH);
    }

    private void addListeners() {
        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(new MyDispatcher());
        btnExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });
    }

    private class MyDispatcher implements KeyEventDispatcher {

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                System.out.println("pressed");
                System.exit(0);
            } else if (e.getID() == KeyEvent.KEY_RELEASED) {
                System.out.println("released");
                System.exit(0);
            } else if (e.getID() == KeyEvent.KEY_TYPED) {
                System.out.println("Typed");
                System.exit(0);
            }
            return false;
        }
    }
}

退出按钮只是因为我厌倦了通过taskmanager杀死我的应用程序。最后,这是我的小组,游戏将在其上进行,我的背景将被绘制:

public class DrawingPanel extends JPanel {

    private final Image background;
    private final SpaceInvaderUI invaderUI;

    DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
        this.background = background;
        this.invaderUI = invaderUI;
    }

    @Override
    protected void paintComponent(Graphics grphcs) {
        super.paintComponent(grphcs);
        grphcs.drawImage(background.getScaledInstance((int) invaderUI.getWidth(), (int) invaderUI.getHeight(), Image.SCALE_SMOOTH), 0, 0, this);
    }
}

提前谢谢。

编辑:我现在尝试在我的drawingPanel上使用键绑定但是当我按下 f2 时仍然没有任何反应:

class SpaceInvaderUI extends Window {

    private JPanel drawingPanel;
    private Image background;
    private JButton btnExit;

    public SpaceInvaderUI(Frame frame) {
        super(frame);
        try {
            background = ImageIO.read(getClass().getResourceAsStream("background.png"));
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Could not extract resource: " + ex.getMessage(), "Error 0x02", JOptionPane.ERROR_MESSAGE);
            System.exit(2);
        }
        createWindow();
    }

    private void createComponents() throws HeadlessException {
        drawingPanel = new DrawingPanel(background, this);
        btnExit = new JButton("Exit");
    }

    private void createWindow() {
        createComponents();
        addListeners();
        addComponentsToWindow();
    }

    private void addComponentsToWindow() {
        add(drawingPanel, BorderLayout.CENTER);
        add(btnExit, BorderLayout.SOUTH);
    }

    private void addListeners() {
        Action exit = new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        };
        drawingPanel.getInputMap().put(KeyStroke.getKeyStroke("F2"),
                exit);
        btnExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                System.exit(0);
            }
        });
    }
}

2 个答案:

答案 0 :(得分:5)

为什么在Swing GUI中使用AWT组件?我担心(但不确定)通过这样做,你可能会失去一些Swing功能。

如果您只是捕捉选择键选择击键来控制游戏,请考虑使用Key Bindings

修改
不,AWT组件没有故障,但仍然可能不应该使用。

编辑2
您的顶级窗口由于某种原因没有集中注意力。继续测试代码...

编辑3
使用JFrame为我工作:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test3 {
   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {

         @Override
         public void run() {
            GraphicsEnvironment ge = GraphicsEnvironment
                  .getLocalGraphicsEnvironment();
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            if (gs.isFullScreenSupported()) {
               SpaceInvaderUI spaceInvaderUI = new SpaceInvaderUI(gs.getDefaultConfiguration());
               gs.setFullScreenWindow(spaceInvaderUI);
            } else {
               JOptionPane.showMessageDialog(null,
                     "Does not support full screen!", "Error 0x01",
                     JOptionPane.ERROR_MESSAGE);
               System.exit(1);
            }
         }
      });
   }
}

// class SpaceInvaderUI extends JWindow {
class SpaceInvaderUI extends JFrame {

   private JPanel drawingPanel;
   private Image background;
   private JButton btnExit;

   public SpaceInvaderUI(GraphicsConfiguration gc) {
      super(gc);
      createWindow();
      addKeyBindings();
      setUndecorated(true);
   }

   private void addKeyBindings() {
      int condition = JPanel.WHEN_IN_FOCUSED_WINDOW;
      InputMap inputMap = drawingPanel.getInputMap(condition );
      ActionMap actionMap = drawingPanel.getActionMap();

      boolean released = false;
      KeyStroke upArrowKeyStrokePressed = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, released );
      String upArrowPressed = "up arrow pressed";
      inputMap.put(upArrowKeyStrokePressed , upArrowPressed);
      actionMap.put(upArrowPressed, new AbstractAction() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            System.out.println("up arrow pressed");
         }
      });

      released = true;
      String upArrowReleased = "up arrow released";
      KeyStroke upArrowKeyStrokeReleased = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, released );
      inputMap.put(upArrowKeyStrokeReleased , upArrowReleased);
      actionMap.put(upArrowReleased , new AbstractAction() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            System.out.println("up arrow released");
         }
      });

   }

   private void createComponents() throws HeadlessException {
      drawingPanel = new DrawingPanel(background, this);
      btnExit = new JButton("Exit");
   }

   private void createWindow() {
      createComponents();
      addListeners();
      addComponentsToWindow();
   }

   private void addComponentsToWindow() {
      add(drawingPanel, BorderLayout.CENTER);
      add(btnExit, BorderLayout.SOUTH);
   }

   private void addListeners() {
//      KeyboardFocusManager manager = KeyboardFocusManager
//            .getCurrentKeyboardFocusManager();
//      manager.addKeyEventDispatcher(new MyDispatcher());
      btnExit.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent ae) {
            System.exit(0);
         }
      });
   }
//
//   private class MyDispatcher implements KeyEventDispatcher {
//
//      @Override
//      public boolean dispatchKeyEvent(KeyEvent e) {
//         System.out.println("in dispatch. KeyEvent := " + e);
//         if (e.getID() == KeyEvent.KEY_PRESSED) {
//            System.out.println("pressed");
//            System.exit(0);
//         } else if (e.getID() == KeyEvent.KEY_RELEASED) {
//            System.out.println("released");
//            System.exit(0);
//         } else if (e.getID() == KeyEvent.KEY_TYPED) {
//            System.out.println("Typed");
//            System.exit(0);
//         }
//         return false;
//      }
//   }
}

class DrawingPanel extends JPanel {

   private final Image background;
   private final SpaceInvaderUI invaderUI;

   DrawingPanel(Image background, SpaceInvaderUI invaderUI) {
      this.background = background;
      this.invaderUI = invaderUI;
      setBackground(Color.pink);
   }

   @Override
   protected void paintComponent(Graphics grphcs) {
      super.paintComponent(grphcs);
   }
}

答案 1 :(得分:4)

如此相关FullScreenTest所示,您可以使用相同的Action实例作为按钮键绑定。

附录:@nIcE cOw要求,我们可以使用 JComponent 在同一 InputMap 上添加多个密钥, ActionMap并使用相同的 AbstractAction

是的,可以使用多个键绑定;我有时会调用doClick()来获取视听反馈,如here所示。