为什么JFrame最大化时会隐藏任务栏? [英] Why jframe hides taskbar when maximized?

查看:83
本文介绍了为什么JFrame最大化时会隐藏任务栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using setUndecorated(true); and getRootPane().setWindowDecorationStyle(JRootPane.FRAME); in my jFrame. This works great but now when I maximized my frame it spreads all over the window even taskbar is not visible. What can I do to make frame not to hide taskbar?

另外,当我最大化最小化帧时,光标会多次更改为此<->,这通常用于当光标位于帧的边界时更改帧的大小.我有什么可以做的吗?

Also when I maximize minimize my frame multiple times the cursor is changed to this <-> which is generally used change size of frame when cursor is on the border of frame. Is there anything I can do for this?

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

A small code then can 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();
    }
}

推荐答案

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

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

从此链接引用:

一种解决方法是将JFrame和 覆盖setExtendedState方法, 在捕获任何最大化事件之前 他们发生并设置最大 适当的框架范围 在调用超类的 setExtendedState方法.

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

这篇关于为什么JFrame最大化时会隐藏任务栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆