Why jframe hides taskbar when maximized?

前端 未结 3 1910
不思量自难忘°
不思量自难忘° 2021-01-17 02:52

I\'m using setUndecorated(true); and getRootPane().setWindowDecorationStyle(JRootPane.FRAME); in my jFrame. This works great but now when I maximiz

3条回答
  •  孤独总比滥情好
    2021-01-17 03:38

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

    Quote from this link:

    A workaround is to subclass JFrame and override the setExtendedState method, catching any maximize events before they happen and setting the maximum bounds of the frame appropriately before calling the superclass's setExtendedState method.

    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);
    }
    }
    

提交回复
热议问题