其他分享
首页 > 其他分享> > 为什么最大化时jframe隐藏任务栏?

为什么最大化时jframe隐藏任务栏?

作者:互联网

我正在使用setUndecorated(true);和getRootPane().setWindowDecorationStyle(JRootPane.FRAME);在我的jFrame中.这很好用,但是现在当我最大化框架时,即使任务栏不可见,它也会散布在整个窗口中.如何使框架不隐藏任务栏?

同样,当我最大化最小化我的帧多次时,光标改变为该<->.通常用于当光标位于边框上时更改边框的大小.我有什么可以做的吗?

一小段代码即可重现该内容:

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

解决方法:

这是一个已知的错误:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4737788

从此链接引用:

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

标签:taskbar,java,swing,jframe
来源: https://codeday.me/bug/20191010/1884701.html