简体   繁体   English

当按f11键时,如何使窗口全屏显示?

[英]when f11 is pressed how can i make the window fullscreen?

import javax.swing.JFrame;

import java.awt.Color;

public class Main extends JFrame{

    public static void main(String[] args) {
        Main window = new Main();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setSize(200, 200);
        window.setVisible(true);
        window.setTitle("Virtual World");
        window.setResizable(true);
        window.getContentPane().setBackground(Color.BLACK);
    }

}

What can I do to make F11 take the window in and out of fullscreen? 如何使F11使窗口进入和退出全屏状态?

I read through other questions and tried to use window.setUndecorated(true); 我通读了其他问题,并尝试使用window.setUndecorated(true); but it didn't seem to do anything... 但它似乎无能为力...

To make a JFrame really fullscreen you must set it undecorated. 要使JFrame真正全屏显示,必须将其设置为未修饰。 But to set it undecorated you must first dispose it. 但是要使其未经装饰,必须首先将其处理。 Eg 例如

class FullscreenToggleAction extends AbstractAction {

  private JFrame frame;
  private GraphicsDevice fullscreenDevice;

  public FullscreenToggleAction (JFrame frame) {
    this(frame, GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
  }

  public FullscreenToggleAction (JFrame frame, GraphicsDevice fullscreenDevice) {
    this.frame = frame;
    this.fullscreenDevice = fullscreenDevice;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    frame.dispose();

    if (frame.isUndecorated()) {
      fullscreenDevice.setFullScreenWindow(null);
      frame.setUndecorated(false);
    } else {
      frame.setUndecorated(true);
      fullscreenDevice.setFullScreenWindow(frame);
    }

    frame.setVisible(true);
    frame.repaint();
  }
}

and then just add the key binding 然后添加键绑定

public class Main {

  public static final void addKeyBinding(JComponent c, String key, final Action action) {
    c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key);
    c.getActionMap().put(key, action);
    c.setFocusable(true);
  }

  public static void main(String[] args) {
    final JFrame frame = new JFrame("Fullscreen Toggle Test");

    Container contentPane = frame.getContentPane();
    contentPane.add(new JLabel("Toogle fullscreen using F11"), BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(600, 400);
    frame.setVisible(true);

    addKeyBinding(frame.getRootPane(), "F11", new FullscreenToggleAction(frame));
  }
}

You can also make it fullscreen on different GraphicsDevice s. 您也可以在其他GraphicsDevice上将其全屏显示。 Eg in a multi monitor environment 例如在多监视器环境中

GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screenDevices = localGraphicsEnvironment.getScreenDevices();

addKeyBinding(frame.getRootPane(), "F11", new FullscreenToggleAction(frame, screenDevices[1]));

I use the following: 我使用以下内容:

public static final void addKeyBinding(JComponent c, String key, final Action action) {
    c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key);
    c.getActionMap().put(key, action);
    c.setFocusable(true);
}

Example: 例:

public static void main(String[] args) {
    final JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JPanel());
    frame.setSize(600, 400);
    frame.setVisible(true);

    addKeyBinding(frame.getRootPane(), "F11", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            int state = frame.getExtendedState();

            if (state == JFrame.MAXIMIZED_BOTH) {
                state = JFrame.NORMAL;
            } else {
                state = JFrame.MAXIMIZED_BOTH;
            }

            frame.setExtendedState(state);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Eclipse上按F11时,Maven自动安装项目 - Maven install project automatically when pressing F11 on eclipse F11不起作用,但ctrl + F11起作用,Java Eclipse - F11 doesn't work but ctrl + F11 works, Java Eclipse 如何从 Eclipse 中删除 Lombok。 与 Eclipse 上的 F11 相关的问题 - How to remove Lombok from Eclipse. Issue related to F11 on Eclipse 在 JavaFX 中使用 F11 键切换全屏功能 - Toggle full screen feature using F11 key in JavaFX 如何使JavaFX确认警报框等到按下OK时,但是在按下其他按钮时执行其他操作 - How can I make a JavaFX confirmation alert box wait till OK is pressed but perform other actions when other buttons are pressed 如何使我的Android对话框全屏显示? - How can I make my android dialog fullscreen? 按下“ S”后,如何使两个椭圆形跟随并环绕另一个椭圆形? - How can i make 2 Ovals follow and circle around another Oval when 'S' is pressed? 当按下键盘按钮而不是循环播放时,如何执行一项操作 - How can I make one action occur when a keyboard button is pressed rather than looping VS F10和F11的eclipse调试预期模拟是什么? - What are eclipse debug prespective analogs for VS F10 and F11? 当我转到第二个窗口时,如何关闭第一个窗口? - How can make the first window close when i go to the second?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM