为什么 jframe 最大化时会隐藏任务栏?
我在 jFrame 中使用 setUndecorated(true);
和 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
。这很好用,但现在当我最大化框架时,它会遍布整个窗口,甚至任务栏也不可见。我该怎么做才能使框架不隐藏任务栏?
此外,当我多次最大化最小化框架时,光标会更改为此 <->
,通常用于当光标位于框架边框上时更改框架大小。为此我能做些什么吗?
然后一个小代码就可以重现这个事情:
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个已知错误: https://bugs.java.com/bugdatabase/view_bug? bug_id=4737788
引用此链接:
This is a known bug: https://bugs.java.com/bugdatabase/view_bug?bug_id=4737788
Quote from this link:
Fortega 的答案有效,但是,某些部分是不需要的(或者 Java 8 不再需要):
GraphicsConfiguration
将发生更改。当考虑双屏配置时,至少在 Windows 上,以下代码无法按预期工作:
在以下双屏设置中:
maxBounds
将包含负 x (-1920),但setMaximizedBounds
不知何故期望屏幕空间中的坐标(其中(x,y)
从(0,0)
开始),而不是虚拟坐标screen:setMaximizedBounds(x=-1920,y=0,width=1920,height=1050)
以下代码适用于具有双屏幕的 Windows:
在简单的
JFrame
上:cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen= 0],pixfmt=0]) 屏幕。{边界: java.awt.Rectangle[x=-1920,y=0,width=1920,height=1080],插图:java.awt.Insets[top=0,left=0,bottom=30,right=0],maxBounds:java.awt.Rectangle[x=0,y=0,width=1920,height=1050]< /code>
cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=1],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1080],插图:java.awt .Insets[顶部=0,左侧=0,底部=30,右侧=0],最大边界: java.awt.矩形[x=0,y=0,宽度=1920,高度=1050]
Fortega answer worked however, some part is not needed (or no longer needed with Java 8):
Rectangle
does not need to be saved.GraphicsConfiguration
will change if the window change screen.setExtendedState
.When factoring dual screen configuration, at least on Windows, the below code does not work as intended:
On the following dual screen set up:
The
maxBounds
will contains negative x (-1920) but thesetMaximizedBounds
is somehow expecting a coordinate in the screen space (where(x,y)
starts at(0,0)
) , not the virtual screen:setMaximizedBounds(x=-1920,y=0,width=1920,height=1050)
The following code work on Windows, with dual screen:
On a simple
JFrame
:cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=0],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=-1920,y=0,width=1920,height=1080], insets: java.awt.Insets[top=0,left=0,bottom=30,right=0], maxBounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1050]
cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=1],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1080], insets: java.awt.Insets[top=0,left=0,bottom=30,right=0], maxBounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1050]
也许您可以设置jFrame的最大尺寸并根据屏幕尺寸进行限制。
编辑
另请查看 设置扩展状态
Maybe you can set the maximum size of the jFrame and restrict it according to the screen size.
EDIT
Also check out setExtendedState
从 Fortega 答案开始,即使添加 125% 屏幕尺寸,您也可以使其正常工作
Starting from Fortega answer, you can make it work even with 125% screen sizi adding