Search code examples
javaeclipseapplet

How to make java applet auto fullscreen? (Eclipse)


Just wondering how I would make the java applet be fullscreen when I run it. I don't want to manually do it with a setSize(); command. I was trying to get it so It would go fullscreen on any monitor regardless of the dimensions. Was just curious if this was possible in eclipse and if so how would I go about doing it.


Solution

  • I meant maximized, im on windows using Eclipse

    Then simply use JFrame#setExtendedState and pass it JFame.MAXIMIZED_BOTH

    As an example...

    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new JLabel("I'm a happy bunny"));
                    // The following two lines just set up the
                    // "default" size of the frame
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    frame.setVisible(true);
                }
            });
        }
    
    }