How can I do full screen in Java on OSX

前端 未结 3 1699
逝去的感伤
逝去的感伤 2020-12-14 23:12

I\'ve been trying and failing to use the java full screen mode on the primary display of an OSX system. Whatever I\'ve tried I can\'t seem to get rid of the \'apple\' menu

3条回答
  •  有刺的猬
    2020-12-14 23:20

    Thats a bit pedantic, the answer is to follow the tutorial completely, which has the essentials and is somewhat more expansive than would fit in a post. The above sample does not work because it is missing a validate(); and some content. I suspect the Java Tutorial will not disappear any time soon. Below is a modified version

    package test;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class FullScreenFrame extends JFrame implements KeyListener {
    
        public FullScreenFrame () {
            addKeyListener(this);
            setUndecorated(true);
            GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    
        if (gd.isFullScreenSupported()) {
            try {
                this.getContentPane().addKeyListener(this);
                this.getContentPane().setLayout(new BorderLayout());
                this.getContentPane().add("Center", new JLabel("Full Screen, back to normal in 10 seconds"));
                gd.setFullScreenWindow(this);
                validate();
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } finally {
                gd.setFullScreenWindow(null);
            }
        } else {
            System.err.println("Full screen not supported");
        }
    
    }
    
    public void keyTyped(KeyEvent e) {
        System.out.println("keyTyped:" +  e.getKeyChar() + "source:"  + e.getSource() );
    }
    public void keyPressed(KeyEvent e) {
        System.out.println("keyPressed:" + e.getKeyChar() + "source:"  + e.getSource() );
    }
    public void keyReleased(KeyEvent e) {
        System.out.println("keyReleased:" + e.getKeyChar() + "source:"  + e.getSource() );
           setVisible(false);
            dispose();
        }
    
        public static void main (String [] args) {
            new FullScreenFrame();
        }
    }
    

提交回复
热议问题