Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing jframe size returns dimension bigger as screen

Tags:

java

swing

While investigating some issue in my application, I just found out some weird 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 1280x1024 resolution monitor, this results in:

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

Anyone knows how this is happening? Especially the fact that the width is higher than as what should happen.

Regards.

like image 211
skiwi Avatar asked Apr 30 '13 16:04

skiwi


People also ask

How do you make a JFrame fit the screen?

Setting a JFrame to fill half the screen For instance, if you want to create a JFrame that is half the width and half the height of the screen, you can just divide the dimensions in half, as shown in this code: Dimension screenSize = Toolkit. getDefaultToolkit(). getScreenSize(); aFrame.

How do I change the size of a JFrame in Java?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.

How do I make my JFrame maximized by default?

By default, we can minimize a JFrame by clicking on minimize button and maximize a JFrame by clicking on maximize button at the top-right position of the screen. We can also do programmatically by using setState(JFrame. ICONIFIED) to minimize a JFrame and setState(JFrame. MAXIMIZED_BOTH) to maximize a JFrame.

What is the default size of JFrame?

First a JFrame is created by invoking its constuctor. The argument to the constructor sets the title of the frame. The setSize(200,100) method makes the rectangular area 200 pixels wide by 100 pixels high. The default size of a frame is 0 by 0 pixels.


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 whole width of the window is then 8+1280+8 pixels = 1296 pixels. The same happens to the height.

When using extended desktop on multiple monitors, one can sometimes notice that when a window is maximized on one screen, the window's border can be seen on adjacent screens.

like image 79
fjf2002 Avatar answered Oct 27 '22 17:10

fjf2002


Not sure what's happening there. If you're just trying to set your frame to it's maximum size based on available screen real estate, try this alternative:

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());
      }
    });
  }
}
like image 27
splungebob Avatar answered Oct 27 '22 17:10

splungebob