How to remove title bar in JFrame(如何在 JFrame 中删除标题栏)
问题描述
我正在使用以下代码进行练习,
I'm using the following code for practising,
http://docs.oracle.com/javase/tutorial/uiswing/examples/layout/BorderLayoutDemoProject/src/layout/BorderLayoutDemo.java
我也加了
frame.setSize(frame.getMaximumSize());
在 createAndShowGUI() 方法中,
in createAndShowGUI() method,
更重要的是,我希望这个窗口没有标题栏、关闭和最小化按钮.
What is more I want this window not to have the title bar, close and minimize buttons.
我试过下面的代码,
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如果我在 pack() 之前添加此代码,它会进入 infine 循环并出现此异常线程AWT-EventQueue-0"java.lang.NegativeArraySizeException 中的异常
If I added this code before the pack(), it goes into infine loop with this exception Exception in thread "AWT-EventQueue-0" java.lang.NegativeArraySizeException
如果我添加了 createAndShowGUI() 方法的最后一行,它会在线程AWT-EventQueue-0"java.awt.IllegalComponentStateException 中抛出异常:该框架是可显示的.
If I added the last line of createAndShowGUI() method it throws Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
我该怎么办?
谢谢.
推荐答案
标题栏可以通过调用setUndecorated(true) 在 Frame 或 JFrame代码>实例,即通过设置未装饰";属性为 true.这会同时删除标题栏和周围的框架.
The title bar can be removed by calling setUndecorated(true) on the Frame or JFrame instance, i.e. by setting the "undecorated" property to true. This removes both the title bar and the surrounding frame.
这是问题所需的代码:
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Already there
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true); // <-- the title bar is removed here
在打包的 JFrame
setUndecorated(true) 必须在 pack() 被直接或间接调用之前调用,例如通过 setVisible(true).
setUndecorated(true) must be called before pack() is being called, either directly or indirectly, for instance through setVisible(true).
显然打包后无法去掉标题栏,这是Frame#setDecorated(boolean):
Apparently it is not possible to remove the title bar after the packing is performed, this is the documentation of Frame#setDecorated(boolean):
框架可以使用 setUndecorated 关闭其原生装饰(即框架和标题栏).这只能在框架不可显示时完成.该方法将抛出运行时异常:IllegalComponentStateException 如果框架是可显示的.
A frame may have its native decorations (i.e. Frame and Titlebar) turned off with
setUndecorated. This can only be done while the frame is not displayable. The method will throw a runtime exception:IllegalComponentStateExceptionif the frame is displayable.
但是,您可以在 Frame 上调用 或 Window#dispose()JFrame 然后再次打包,这会导致 JFrame 内的组件布局被刷新.
However, you can call Window#dispose() on the Frame or JFrame and then pack it again, which causes the layout of the components within the JFrame to be refreshed.
这篇关于如何在 JFrame 中删除标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 JFrame 中删除标题栏
- 如何指定 CORS 的响应标头? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
