Mac: Full Screen JFrame losing keybindings from mouseadapter

[亡魂溺海] 提交于 2019-12-25 09:35:17

问题


On my Mac, fullscreen JFrames initially have key bindings that do not work, and the computer outputs alert beeps each time I try to type. There is a workaround, though, after fully initializing my JFrame, I added these lines of code and all the errors stopped:

   setVisible(false);
   setVisible(true);

Here's the source of this workaround: http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html

Another problem which is yet to be solved is adding a mouse adapter to my full screen JFrame application. Whenever I clicked, the focus changed--to where, I couldn't quite tell, but setting the inputmap of my keybindings to each one of the three options didn't help.

I even tried redoing the workaround when the mouse was clicked by adding this: event.getComponent().setVisible(false); event.getComponent().setVisible(true); but to no avail.

Here is an SSCCE of the problem (it will only show up on a mac):

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class FocusTest extends JFrame{
    private static final int PREF_W = 400;
    private static final int PREF_H = PREF_W;

    public FocusTest() {
       MyPanelDescendent myPanelDescendent = new MyPanelDescendent();
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       getContentPane().add(myPanelDescendent);
       pack();
       setLocationByPlatform(true);
       setVisible(true);

       KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
       Action escapeAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
             dispose();
             System.exit(0);
            }
       };
       getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(escapeKeyStroke, "ESCAPE");
       getRootPane().getActionMap().put("ESCAPE", escapeAction);

       GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
       GraphicsDevice gs = ge.getDefaultScreenDevice();
       gs.setFullScreenWindow(this);

       setVisible(false);
       setVisible(true);
    }
    private class MyPanelAscendent extends JPanel{
        public MyPanelAscendent() {
           setFocusable(true);
           requestFocusInWindow();

             getInputMap(0).put(KeyStroke.getKeyStroke("pressed A"), "pressed");
             getActionMap().put("pressed", new AbstractAction() {
                   @Override public void actionPerformed(ActionEvent e) {
                       if (e.getActionCommand().equalsIgnoreCase("a")) {
                           System.out.println("a was pressed");
                       }
                   }
             });

           addMouseListener(new MyAdapter());
        }
    }
    private class MyPanelDescendent extends MyPanelAscendent {
        public MyPanelDescendent() {
            super();
        }
    }

    private class MyAdapter extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent event) {
            event.getComponent().setVisible(false);
            event.getComponent().setVisible(true);
            System.out.println("clicked");
        }
    }

    @Override
    public Dimension getPreferredSize() {
       return new Dimension(PREF_W, PREF_H);
    }



    public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
          public void run() {
             new FocusTest();
          }
       });
    }
}

If you press the a key, then click, then do it again, it will not work. The same goes for the escape key: if you click then try to use it, it won't work.

here is an example for fullscreen posted by trashgod which I've also found impossible to make work with both keybindings, fullscreen, and a mouse adapter at the same time.

来源:https://stackoverflow.com/questions/19762647/mac-full-screen-jframe-losing-keybindings-from-mouseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!