JFrame 最大化窗口
我正在使用 swing 制作一个快速而肮脏的动画。 我希望窗口最大化。 我怎样才能做到这一点?
I'm putting together a quick and dirty animation using swing. I would like the window to be maximized. How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我已经尝试过此线程中的解决方案以及此处的解决方案,但只需调用
setExtendedState(getExtendedState()|调用
显然不适用于我的环境(Windows 10、JDK 1.8,我的任务栏位于屏幕右侧)。 这样做仍然在左侧、右侧和底部留下了微小的空间。setVisible(true);
后立即调用 Frame.MAXIMIZED_BOTH);然而,对我有用的是,在激活窗口时调用
setExtendedState(...
),如下所示:I've tried the solutions in this thread and the ones here, but simply calling
setExtendedState(getExtendedState()|Frame.MAXIMIZED_BOTH);
right after callingsetVisible(true);
apparently does not work for my environment (Windows 10, JDK 1.8, my taskbar is on the right side of my screen). Doing it this way still leaves a tiny space on the left, right and bottom .What did work for me however, is calling
setExtendedState(...
when the window is activated, like so:我最终使用了这段代码:
此选项是所有选项中效果最好的,包括多显示器支持。 唯一的缺陷是任务栏偏移在所有显示器上都使用某些配置。
I ended up using this code:
This options worked the best of all the options, including multiple monitor support. The only flaw this has is that the taskbar offset is used on all monitors is some configurations.
@kgiannakakis 的答案完全正确,但如果有人陷入这个问题并在 Linux 上使用 Java 6(例如 Mint 19 Cinnamon),MAXIMIZED_BOTH 状态有时不适用。
您可以尝试调用 pack()设置此状态后的方法。
代码示例:
如果您在 Windows 上使用 Java 7+ 或 Java 6,则无需执行此操作。
@kgiannakakis answer is fully correct, but if someone stuck into this problem and uses Java 6 on Linux (by example, Mint 19 Cinnamon), MAXIMIZED_BOTH state is sometimes not applied.
You could try to call pack() method after setting this state.
Code example:
This is not necessary if you are using Java 7+ or Java 6 on Windows.
假设您要扩展 JFrame:
Provided that you are extending JFrame:
类似于 this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
Something like
this.setExtendedState(this.getExtendedState() | this.MAXIMIZED_BOTH);
如果您使用 JFrame,请尝试此操作
If you're using a JFrame, try this
JFrame.setExtendedState 怎么样(JFrame.MAXIMIZED_BOTH)
?How about
JFrame.setExtendedState(JFrame.MAXIMIZED_BOTH)
?我喜欢这个版本:
i like this version:
将 JFrame 设置为全屏的方法是设置
MAXIMIZED_BOTH
选项代表MAXIMIZED_VERT | noreferrer">
,分别设置框架垂直和水平最大化MAXIMIZED_BOTH
选项 MAXIMIZED_HORIZThe way to set JFrame to full-screen, is to set
MAXIMIZED_BOTH
option which stands forMAXIMIZED_VERT | MAXIMIZED_HORIZ
, which respectively set the frame to maximize vertically and horizontally