Size jframe java swing returns a size larger than the screen

While researching some kind of problem in my application, I just discovered some strange thing.

Basically this SSCCE should demonstrate the problem:

public class MainFrame extends JFrame {
    public MainFrame() {
         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
         setExtendedState(JFrame.MAXIMIZED_BOTH);
         pack();
    }
}

public class Main {
    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
        mf.setVisible(true);
        System.out.println(mf.getSize());
    }
}

Somehow on my monitor with a resolution of 1280x1024 this leads to:

java.awt.Dimension [width = 1296 height = 1010]

Does anyone know how this happens? Especially the fact that the width is higher than what should happen.

Sincerely.

+5
source share
2 answers

The window border is probably 8 pixels wide. When maximizing, Windows resizes the window so that the client area gets 1280 pixels wide. The entire width of the window is 8 + 1280 + 8 pixels = 1296 pixels. The same thing happens with height.

, , .

+5

, . , :

import java.awt.*;
import javax.swing.*;

public class JFrameExtended
{
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable()
    {
      public void run()
      {
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Rectangle maxBounds = env.getMaximumWindowBounds();

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBounds(maxBounds);
        f.setVisible(true);
        System.out.println("Frame size: " + f.getSize());
      }
    });
  }
}
+2

All Articles