Swing手動進行最大化最小化


首先jdk的setExtendedState是有bug的,需要先重載JFrame的setExtendedState方法

    /**
     * Fix the bug "jframe undecorated cover taskbar when maximized". See:
     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788
     *
     * @param state
     */
    @Override
    public void setExtendedState(int state) {

        if ((state & java.awt.Frame.MAXIMIZED_BOTH) == java.awt.Frame.MAXIMIZED_BOTH) {
            Rectangle bounds = getGraphicsConfiguration().getBounds();
            Rectangle maxBounds = null;
            // Check to see if this is the 'primary' monitor
            // The primary monitor should have screen coordinates of (0,0)
            if (bounds.x == 0 && bounds.y == 0) {
                Insets screenInsets = getToolkit().getScreenInsets(getGraphicsConfiguration());
                maxBounds = new Rectangle(screenInsets.left, screenInsets.top, bounds.width - screenInsets.right - screenInsets.left
                        , bounds.height - screenInsets.bottom - screenInsets.top);
            } else {
                // Not the primary monitor, reset the maximized bounds...
                maxBounds = null;
            }
            super.setMaximizedBounds(maxBounds);
        }
        super.setExtendedState(state);
    }

  

然后,最大化的時候就設置:

setExtendedState(Frame.MAXIMIZED_BOTH);

 

但是,最小化的時候,需要注意,設置成:

super.setExtendedState(Frame.ICONIFIED | getExtendedState());

否則,假如直接設置:super.setExtendedState(Frame.ICONIFIED);

還原的時候Sate在~Frame.ICONIFIED之后成了0,就變成了NORMAL態,這樣是不對的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM