Why does jframe hide the taskbar while maximizing?

I am using setUndecorated(true); and getRootPane().setWindowDecorationStyle(JRootPane.FRAME); in your jFrame. This works fine, but now that I have maximized my frame, it spreads throughout the window, even the taskbar is not visible. What can I do to make a frame, so as not to hide the taskbar?

Also, when I maximize the minimization of my frame several times, the cursor changes to this <-> , which is usually used to resize the frame when the cursor is at the border of the frame. What can I do for this?


A small code can then reproduce the thing:

 import javax.swing.JFrame; import javax.swing.JRootPane; public class Demo extends JFrame { public Demo() { setSize(250,125); setUndecorated(true); getRootPane().setWindowDecorationStyle(JRootPane.FRAME); setVisible(true); } public static void main(String[] args) { new Demo(); } } 
0
source share
3 answers

This is a known bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788

Quote from this link:

The workaround is to subclass the JFrame and override the setExtendedState method, catch any they occur and set the maximum frame boundaries accordingly before calling the setExtendedState superclass.

 import java.awt.*; import javax.swing.*; public class PFrame extends JFrame { private Rectangle maxBounds; public PFrame() { super(); maxBounds = null; } //Full implementation has other JFrame constructors public Rectangle getMaximizedBounds() { return(maxBounds); } public synchronized void setMaximizedBounds(Rectangle maxBounds) { this.maxBounds = maxBounds; super.setMaximizedBounds(maxBounds); } public synchronized void setExtendedState(int state) { if (maxBounds == null && (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) { Insets screenInsets = getToolkit().getScreenInsets(getGraphicsConfiguration()); Rectangle screenSize = getGraphicsConfiguration().getBounds(); Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, screenInsets.top + screenSize.y, screenSize.x + screenSize.width - screenInsets.right - screenInsets.left, screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top); super.setMaximizedBounds(maxBounds); } super.setExtendedState(state); } } 
+5
source

Perhaps you can set the maximum jFrame size and limit it to fit the screen size.

EDIT

Also check setExtendedState

0
source

Fortega's answer worked, however, some part is not needed (or is no longer needed with Java 8):

  • Rectangle does not need to be saved.
  • The code does not take into account dual screen configuration. In particular, GraphicsConfiguration will change if the window changes screen.
  • As far as I tested, the only mandatory override is setExtendedState .

When calculating a dual-screen configuration, at least on Windows , the code below does not work properly:

 Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, screenInsets.top + screenSize.y, screenSize.x + screenSize.width - screenInsets.right - screenInsets.left, screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top); 

On the following dual screen, configure:

  • Left screen 1920x1080 (not primary), position: -1920, 0
  • Right screen 1920x1080 (main), position: 0, 0

The maxBounds field will contain negative x (-1920), but setMaximizedBounds somehow expects a coordinate in the screen space (where (x,y) starts with (0,0) ), not the virtual screen:

  • It will be setMaximizedBounds(x=-1920,y=0,width=1920,height=1050)
  • Windows will see the window on the left screen (because I have one taskbar per screen, showing only the window on this screen), however, the window will not be displayed on the screen because it is outside.
  • If the screen resolution or, even worse, its scale factor (on a Windows 10 laptop will use a scale factor, for example: 25%, which makes the screen β€œnot very” 1920x1080), then the above code does not adapt. For example, if there are 3 screens in my configuration, and the right-most of them is the main one, the window will not display well on the left and middle screens. I do not think I fixed this in the code below.

The following code works on dual-screen Windows:

  @Override public synchronized void setExtendedState(final int state) { if ((state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) { final GraphicsConfiguration cfg = getGraphicsConfiguration(); final Insets screenInsets = getToolkit().getScreenInsets(cfg); final Rectangle screenBounds = cfg.getBounds(); final int x = screenInsets.left + screenBounds.x * 0; final int y = screenInsets.top + screenBounds.y * 0; final int w = screenBounds.width - screenInsets.right - screenInsets.left; final int h = screenBounds.height - screenInsets.bottom - screenInsets.top; final Rectangle maximizedBounds = new Rectangle(x, y, w, h); System.out.println("cfg (" + cfg + ") screen.{bounds: " + screenBounds + ", insets: " + screenInsets + ", maxBounds: " + maximizedBounds); super.setMaximizedBounds(maximizedBounds); } super.setExtendedState(state); } 

On a simple JFrame :

  • When maximized, the left screen ("screen = 0") will have cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=0],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=-1920,y=0,width=1920,height=1080], insets: java.awt.Insets[top=0,left=0,bottom=30,right=0], maxBounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1050]
  • When maximized, the right screen ("screen = 1") will have cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=1],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1080], insets: java.awt.Insets[top=0,left=0,bottom=30,right=0], maxBounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1050]
0
source

Source: https://habr.com/ru/post/1445879/


All Articles