0

Basically I want to control the size of the content pane even when the application is in full-screen exclusive mode. I want the content to be square, with black bars taking up unused space. Is there a way to do this? Setting the preferred size doesn't work, it just fills the screen anyway. Here's the gist of my code:

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setBackground(Color.BLACK);

    setPreferredSize(Toolkit.getDefaultToolkit().getScreenSize());
    setUndecorated(true);
    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);

    getContentPane().setPreferredSize(new Dimension(getHeight(), getHeight()));
    getContentPane().setBackground(Color.WHITE);

    pack();

This results in the entire screen being white. On my widescreen monitor, I want the middle of the screen to be white, with black bars on the sides. Is there a clean way to do this? I guess I could add a secondary JPanel, size it correctly, and use that for everything, but that seems sloppy. I'd rather use the JFrame's built-in content pane.

Andrew0085
  • 85
  • 1
  • 4
  • 1
    If you're using "Full Screen Exclusive Mode", you can't, that's the point. If you're just trying to size the frame, then allow it to be maximised (use `setExtenededState`) and then control the content, using `getPreferredSize` on your component and something like a `GridBagLayout` which will honour the constraints – MadProgrammer May 02 '15 at 22:13
  • See [Making a robust, resizable Swing Chess GUI](http://stackoverflow.com/q/21142686/418556). It forces the chess board to a square size at any frame size. – Andrew Thompson May 03 '15 at 02:37

0 Answers0