【问题标题】:when f11 is pressed how can i make the window fullscreen?当按下 f11 时如何使窗口全屏显示?
【发布时间】:2016-03-07 14:51:19
【问题描述】:
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);
    }

}

如何使 F11 使窗口进入和退出全屏模式?

我阅读了其他问题并尝试使用window.setUndecorated(true);,但它似乎没有做任何事情......

【问题讨论】:

    标签: java swing jframe window fullscreen


    【解决方案1】:

    要使JFrame 真正全屏,您必须将其设置为未装饰。但要将其设置为未装饰,您必须先将其处理掉。例如

    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();
      }
    }
    

    然后只需添加键绑定

    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));
      }
    }
    

    您还可以在不同的GraphicsDevices 上使其全屏显示。例如。在多显示器环境中

    GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] screenDevices = localGraphicsEnvironment.getScreenDevices();
    
    addKeyBinding(frame.getRootPane(), "F11", new FullscreenToggleAction(frame, screenDevices[1]));
    

    【讨论】:

      【解决方案2】:

      我使用以下内容:

      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("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);
              }
          });
      }
      

      【讨论】:

      • 这可行,但我希望窗口覆盖整个屏幕。例如看电影全屏看不到任务栏
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      相关资源
      最近更新 更多