如何删除JFrame中的标题栏
我使用以下代码进行练习,
我还添加了
frame.setSize(frame.getMaximumSize());
createAndShowGUI()方法,
而且我希望这个窗口没有标题栏、关闭和最小化按钮。
我尝试了以下代码,
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如果我在 pack() 之前添加此代码,它将进入无限循环并出现此异常 线程“AWT-EventQueue-0”java.lang.NegativeArraySizeException 中的异常
如果我添加createAndShowGUI() 方法的最后一行它抛出 线程“AWT-EventQueue-0”中的异常 java.awt.IllegalComponentStateException: The Frame is displayedable.
我应该做什么?
谢谢。
I'm using the following code for practising,
I also add
frame.setSize(frame.getMaximumSize());
in createAndShowGUI() method,
What is more I want this window not to have the title bar, close and minimize buttons.
I tried the following code,
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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
If I added the last line of createAndShowGUI() method it throws Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: The frame is displayable.
What should I do ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以通过调用 来删除标题栏
Frame
或JFrame
实例上的setUndecorated(true)
,即通过将“undecorated”属性设置为true< /代码>。这将删除标题栏和周围的框架。
以下是该问题所需的代码:
在打包的
JFrame
上,必须在
pack()
setUndecorated(true) > 被直接或间接调用,例如通过setVisible(true)
。显然,在执行打包后不可能删除标题栏,这是
Frame#setDecolated(boolean)
:但是,您可以在
FrameWindow#dispose()
> 或JFrame
,然后再次打包,这会导致JFrame
内组件的布局被刷新。The title bar can be removed by calling
setUndecorated(true)
on theFrame
orJFrame
instance, i.e. by setting the "undecorated" property totrue
. This removes both the title bar and the surrounding frame.Here's the required code for the question:
On a packed
JFrame
setUndecorated(true)
must be called beforepack()
is being called, either directly or indirectly, for instance throughsetVisible(true)
.Apparently it is not possible to remove the title bar after the packing is performed, this is the documentation of
Frame#setDecorated(boolean)
:However, you can call
Window#dispose()
on theFrame
orJFrame
and then pack it again, which causes the layout of the components within theJFrame
to be refreshed.好吧,
createAndShowGUI()
中的以下代码片段对我有用:请注意,我不确定您想要通过手动将未实现的框架的大小设置为其最大大小来实现什么目的,这最初将为
(0, 0)
。Well, the following code snippet in
createAndShowGUI()
worked for me:Note that I'm not sure what you're trying to achieve by manually setting the size of an unrealized frame to it's maximum size, which will be
(0, 0)
initially.