1

我正在尝试在基于此代码的 Java APP 中实现全屏模式

private static void createAndShowGUI()
{
    //Create and set up the window.
    JFrame frame = new JFrame("Mouse Click Demo");
    //Make it non-Resizable
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JComponent newContentPane = new MouseEventDemo();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    //frame.setVisible(true);
    if (!frame.isDisplayable())
    {
        // Can only do this when the frame is not visible
        frame.setUndecorated(true);
    }
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    try
    {
      if(gd.isFullScreenSupported())
      {
         gd.setFullScreenWindow(frame);
         System.out.print("DOES SUPPORT");
      }
      else
      {
          //Can't run in full screen mode
          System.out.print("DOES NOT SUPPORT");
      }
      frame.setVisible(true);
    }
    finally
    {
     gd.setFullScreenWindow(null);
    }

}

问题是,当我在 Macbook 上运行时,它仍然显示顶部 Mac 栏和底部甲板,控制台确实进入状态并打印DOES SUPPORT但仍不显示在全屏模式下。

我在做什么错?

编辑:这是我的屏幕截图

4

2 回答 2

3

回应您的Escape评论...

你最好的选择是使用键绑定

有很多原因,但最主要的原因是它没有受到排他焦点问题的KeyListener影响

public class TickleMe {

    public static void main(String[] args) {
        new TickleMe();
    }

    public TickleMe() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                final JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setUndecorated(true);

                JPanel content = new JPanel(new GridBagLayout());
                content.add(new JLabel("Tickle Me"));
                InputMap im = content.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
                ActionMap am = content.getActionMap();
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "getMeOutOfHere");
                am.put("getMeOutOfHere", new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        try {
                            GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                            gd.setFullScreenWindow(null);
                        } finally {
                            frame.dispose();
                        }
                    }
                });

                frame.setContentPane(content);

                GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                if (gd.isFullScreenSupported()) {
                    gd.setFullScreenWindow(frame);
                    frame.requestFocusInWindow();
                }

            }
        });
    }
}
于 2013-02-19T06:20:41.893 回答
0

好的,找到了罪魁祸首,示例最终调用它,它再次进入正常窗口。我不得不重新启动我的机器,因为 ESC 也无法退出全屏模式。

于 2013-02-19T05:31:40.733 回答