How do you make key bindings for a java.awt.Frame?

后端 未结 2 488
猫巷女王i
猫巷女王i 2020-12-21 06:55

Background

My window is a java.awt.Frame, and inside of the Frame are two Panels (java.awt.Panel). I\'m trying to make it so that the window handles buttons I pres

相关标签:
2条回答
  • 2020-12-21 07:26

    Okay, here's an example using a JPanel.

    I created a Frame, set it's layout to BorderLayout added to the KeyPane to it and voila...

    public class KeyPane extends JPanel {
    
        private Timer paintTimer;
        private MouseFocusHandler mouseFocusHandler;
    
        private boolean spaceOn = false;
        private int yPos = 0;
        private int direction = 2;
    
        private Rectangle blob = new Rectangle(0, 0, 10, 10);
    
        public KeyPane() {
    
            setFocusable(true);
    
            InputMap im = getInputMap(WHEN_FOCUSED);
            ActionMap am = getActionMap();
    
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "Space");
            am.put("Space", new SpaceAction());
    
            setPreferredSize(new Dimension(100, 100));
    
            getPaintTimer().setCoalesce(false);
            getPaintTimer().start();
    
        }
    
        @Override
        public void addNotify() {
            super.addNotify();
            requestFocusInWindow();
            addMouseListener(getMouseFocusHandler());
            getPaintTimer().start();
        }
    
        @Override
        public void removeNotify() {
            removeMouseListener(getMouseFocusHandler());
            getPaintTimer().stop();
            super.removeNotify();
        }
    
        protected Timer getPaintTimer() {
            if (paintTimer == null) {
                paintTimer = new Timer(40, new RepaintAction());
                paintTimer.setRepeats(true);
                paintTimer.setCoalesce(true);
            }
            return paintTimer;
        }
    
        protected MouseFocusHandler getMouseFocusHandler() {
            if (mouseFocusHandler == null) {
                mouseFocusHandler = new MouseFocusHandler();
            }
            return mouseFocusHandler;
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
    
            int width = getWidth() - 1;
            int height = getHeight() - 1;
    
            g2d.setColor(Color.GREEN);
    
            blob.x = (width - blob.width) / 2;
    
            System.out.println(blob);
            g2d.fill(blob);
    
            if (spaceOn) {
                g2d.setFont(UIManager.getFont("Label.font").deriveFont(24f));
                FontMetrics fm = g2d.getFontMetrics();
    
                String spaceIsOn = "Space On";
    
                int x = (width - fm.stringWidth(spaceIsOn)) / 2;
                int y = ((height - fm.getHeight()) / 2) + fm.getAscent();
    
                g2d.drawString(spaceIsOn, x, y);
            }
    
            g2d.dispose();
    
        }
    
        protected class MouseFocusHandler extends MouseAdapter {
    
            @Override
            public void mouseClicked(MouseEvent e) {
                requestFocusInWindow();
            }
    
        }
    
        protected class RepaintAction implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                yPos += direction;
                blob.y = yPos;
    
                if (blob.y + blob.height > getHeight() - 1) {
                    blob.y = getHeight() - 1 - blob.height;
                    direction = -2;
                } else if (blob.y < 0) {
                    blob.y = 0;
                    direction = 2;
                }
                repaint();
            }
    
        }
    
        protected class SpaceAction extends AbstractAction {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                spaceOn = !spaceOn;
                repaint();
            }
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-21 07:33

    I found that I could do this with an AWTEventListener.

    public class MyFrame extends Frame implements AWTEventListener {
    
      ...
    
      public MyFrame(String title){
        super(title);
        ...
        this.getToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
      }
    
      @Override
      public void eventDispatched(AWTEvent event) {
        if(event instanceof KeyEvent){
          KeyEvent key = (KeyEvent)event;
          if(key.getID()==KeyEvent.KEY_PRESSED){ //Handle key presses
            System.out.println(key.getKeyChar());
            //TODO: do something with the key press
            key.consume();
          }
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题