如何在 JFrame 中删除标题栏 [英] How to remove title bar in JFrame

查看:28
本文介绍了如何在 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)FrameJFrame实例,即通过设置未装饰";属性为 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: IllegalComponentStateException if 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 中删除标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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