SetSize () does not work for JFrame

import javax.swing.JFrame; import javax.swing.SwingUtilities; class Demo { JFrame jf; Demo() { jf=new JFrame("Demo"); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(5000,5000); jf.setVisible(true); System.out.println(jf.getSize()); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new Demo(); } }); } } 

I use jf.setSize(5000, 5000) for a JFrame, but after that getSize returns a different size: java.awt.Dimension[width=1386,height=788] (screen resolution is 1366x768) Can I set the frame size to be larger than Screen size? Perhaps this behavior is provided by some properties of the frame, but I do not know about them.

+8
java swing jframe
source share
5 answers

Try using setPreferredSize instead of setSize. Works in my case (Ubuntu 12.04 + GNOME 3).

+10
source share

Javadoc says the following:

"The method changes the geometry-related data. Therefore, the native window system can ignore such requests or can modify the requested data so that the Window object is placed and set in such a way that it matches the configuration desktop."

This covers the behavior you are observing / complaining.

This is not crystal clear, but one of the reasons Window.setSize(...) has this behavior is because window managers (outside of Java) tend to veto application attempts. Presumably, this is because it is open to abuse and "not what the user wants." In any case, ultimately it is not your application that causes the override of restrictions on the window manager.

+7
source share

Just for fun on Friday (that is, it doesn’t cost anything to do in the production environment :-) - playing a bit further with @jinguy code, I noticed that the file size is larger than it was used after minimizing the frame. Performing this programmatically, let him appear like a monster from the start

  jf.setPreferredSize(new Dimension(5000,5000)); jf.setMinimumSize(new Dimension(5000,5000)); jf.pack(); jf.setVisible(true); jf.setState(Frame.ICONIFIED); jf.setState(Frame.NORMAL); System.out.println(jf.getSize()); 
+4
source share

I tried several combinations of calls, and it seemed to work:

 jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setPreferredSize(new Dimension(5000,5000)); jf.setMaximumSize(new Dimension(5000,5000)); jf.setMinimumSize(new Dimension(5000,5000)); jf.pack(); jf.setVisible(true); 

It still prints a different size, but the window is much larger than it prints.

+3
source share

These are my observations (using 1.6, now I use 1.7 under XP):

You can have a limited frame of "almost" any size - I use a screen resolution of 1280x1024 (rotated) and did not notice any problems with the 1500x1500 frame, although some 2000x2000 frames look incomplete (but work) and a 4000x4000 frame displays the thumb on the taskbar, but this thumb is inactive and the frame itself is not displayed. I think the largest unecorated JFrame size depends on system capabilities, which depend on the graphics hardware.

There is a simple story with decorated frames - they can be slightly larger than the screen size (by a few pixels in general).

In my application with a size determined at runtime (for example, in games where you set the board size dynamically), I use the following approach:

one). Before packing, set the frame to zero. It puts the top left corner of the JFrame in the middle of the screen (before the JFrame package is (0,0))

2). set the preferred dimensions of the contents of my frame (I always use one JPanel) and remember them

3). pack frame

4). if the frame sizes after the package do not match the dimensions before packing, delete the contents of the JPanel, add a JScrollPane with this JPanel and set the preferred JScrollPane sizes as the preferred JPanel sizes. PLUS the fixed dimensions of the JScrollBar (i.e. the width of the vertical scrollbar and the height of the horizontal).

5). pack again - this ensures that only the necessary scroll bars appear (if you do not increase the size of the JFrame, then the scroll bars appear), you also need to remove the default JScrollPane border).

6). Set a new location for the frame by moving it left and up half the size to center it.

+1
source share

All Articles