gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:35:55 25 4
gpt4 key购买 nike

我正在使用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);
}
}

关于java - 为什么 jframe 最大化时会隐藏任务栏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6422931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com